这是我的主要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')),
]
这些是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'),
]
我正在使用rest_framework.documentation。 奇怪的是,只有登录和注销视图显示在文档主页中。 这些是由网址路由到的相关视图。这是登录视图
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)
这是退出视图,
class LogoutView(views.APIView):
def get(self, request):
logout(request)
return Response({}, status=status.HTTP_204_NO_CONTENT)
这是注册视图,
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, 'soumasish@foottheball.com']
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))
这是位置视图,
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)
最后两个观点没有显示出来。 有人可以帮助解决这里发生的事情吗?
答案 0 :(得分:1)
您必须在最后两个视图中实施post()
或CamelTestSupport
方法,否则它们不是APIView的有效视图,如下所述:http://www.django-rest-framework.org/api-guide/views/
答案 1 :(得分:0)
您是否尝试使用路由器注册网址?
例如:
from rest_framework import routers
routes = routers.DefaultRouter()
routes.register(r'location', LocationView)
urlpatterns += api_routes.urls
答案 2 :(得分:0)
将RegisterView
和LocationView
替换为以下
class RegisterView(views.APIView):
def get_or_create(self, request, **kwargs):
# Your Code
def _generate(self):
return ''.join(random.choice('0123456789ABCDEF') for _ in range(32))
def get(self,request):
return self.get_or_create(request)
class LocationView(views.APIView):
def get_or_create(self, request):
# Your Code
def get(self, request):
return self.get_or_create(request)