Reputation: 3605
This is my main urls.py
urlpatterns = [
url(r'^', include_docs_urls(title='Foot The Ball API')),
url(r'^admin/', admin.site.urls),
url(r'^api/v1/aggregator/', include('aggregator.urls')),
url(r'^api/v1/bouncer/', include('bouncer.urls')),
]
These are the urls in bouncer.urls
urlpatterns = [
url(r'^login/$', LoginView.as_view(), name='login'),
url(r'^logout/$', LogoutView.as_view(), name='logout'),
url(r'^register/(?P<location_id>[\w.-]+)/$', RegisterView.as_view(), name='register'),
url(r'^location/$', LocationView.as_view(), name='location'),
]
I'm using rest_framework.documentation. Strangely only login and logout view show up in the documentation home page. These are the relevant views that are routed to by the urls. This is the login view
class LoginView(views.APIView):
def post(self, request):
user = authenticate(
email=request.data.get("email"),
password=request.data.get("password")
)
if not user:
return Response({
'status': 'Unauthorized',
'message': 'Email or password incorrect',
}, status=status.HTTP_401_UNAUTHORIZED)
login(request, user)
return Response(UserSerializer(user).data)
This is the logout view,
class LogoutView(views.APIView):
def get(self, request):
logout(request)
return Response({}, status=status.HTTP_204_NO_CONTENT)
This is the Register View,
class RegisterView(views.APIView):
def get_or_create(self, request, **kwargs):
try:
user = User.objects.get(email=request.data.get("email"))
return Response(user, status=status.HTTP_200_OK)
except User.DoesNotExist:
location = Location.objects.get(id=kwargs.get('location_id'))
serializer = UserSerializer(data=request.data)
if serializer.is_valid():
user = serializer.save()
user.location = location
user.save()
subject = "Please Activate Your FootTheBall Account!"
token = self._generate()
link = HOST + PREFIX + str(user.id) + SUFFIX + token
message = 'Please use the following link to activate your account.\n\n{}'.format(link)
from_email = settings.EMAIL_HOST_USER
to_list = [user.email, '[email protected]']
send_mail(subject, message, from_email, to_list, fail_silently=True)
Token.objects.create(user=user, token=token)
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def _generate(self):
return ''.join(random.choice('0123456789ABCDEF') for _ in range(32))
And this is the location view,
class LocationView(views.APIView):
def get_or_create(self, request):
try:
location = Location.objects.get(country=request.data.get("country"), city=request.data.get("city"))
Response(location, status=status.HTTP_200_OK)
except Location.DoesNotExist:
serializer = LocationSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
The last two views dont show up. Can someone help as to what's going on here?
Upvotes: 1
Views: 1540
Reputation: 37876
you must implement get()
or post()
methods in the last two views otherwise they are not valid views for APIView as stated here: http://www.django-rest-framework.org/api-guide/views/
Upvotes: 1
Reputation: 118
Did you try to register the urls with Routers?
e.g:
from rest_framework import routers
routes = routers.DefaultRouter()
routes.register(r'location', LocationView)
urlpatterns += api_routes.urls
documentation: http://www.django-rest-framework.org/api-guide/routers/
Upvotes: 0