英文:
Django url not hitting the right endpoint
问题
views.py:
from django.shortcuts import render
from django.http import HttpResponse
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
import requests
# Create your views here.
def index(request):
return render(request, 'index.html')
class ActivateUser(GenericAPIView):
def get(self, request, uid, token, format=None):
payload = {'uid': uid, 'token': token}
url = "http://localhost:8000/api/auth/users/activation/"
response = requests.post(url, data=payload)
if response.status_code == 204:
return Response({}, response.status_code)
else:
return Response(response.json())
urls.py:
from django.contrib import admin
from django.urls import path, include, re_path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('account.urls')),
path('', views.index, name='index'),
re_path(r'^activate/(?P<uid>[\w-]+)/(P<token>[\w-]+)/$', views.ActivateUser.as_view())
]
error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000//activate/MjU/brgs37-d7dee487798f432dc9fb24478dae100b
Using the URLconf defined in artist_portal.urls, Django tried these URL patterns, in this order:
admin/
api/
[name='index']
^activate/(?P
[\w-]+)/(P [\w-]+)/$ The current path, /activate/MjU/brgs37-d7dee487798f432dc9fb24478dae100b, didn't match any of these.
英文:
views.py
from django.shortcuts import render
from django.http import HttpResponse
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
import requests
# Create your views here.
def index(request):
return render(request, 'index.html')
class ActivateUser(GenericAPIView):
def get(self, request, uid, token, format = None):
payload = {'uid': uid, 'token': token}
url = "http://localhost:8000/api/auth/users/activation/"
response = requests.post(url, data = payload)
if response.status_code == 204:
return Response({}, response.status_code)
else:
return Response(response.json())
urls.py
from django.contrib import admin
from django.urls import path, include, re_path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('account.urls')),
path('', views.index, name='index'),
re_path(r'^activate/(?P<uid>[\w-]+)/(?P<token>[\w-]+)/$', views.ActivateUser.as_view())
]
error:
> Page not found (404)
>
> Request Method: GET
>
> Request URL: http://127.0.0.1:8000//activate/MjU/brgs37-d7dee487798f432dc9fb24478dae100b
>
> Using the URLconf defined in artist_portal.urls, Django tried these URL patterns, in this order:
>
> admin/
>
> api/
>
> [name='index']
>
> ^activate/(?P<uid>[\w-]+)/(?P<token>[\w-]+)/$
>
> The current path, /activate/MjU/brgs37-d7dee487798f432dc9fb24478dae100b, didn’t match any of these.
答案1
得分: 0
在你的urls.py文件中,你已经定义了激活URL模式如下:
re_path(r'^activate/(?P<uid>[\w-]+)/(?P<token>[\w-]+)/$', views.ActivateUser.as_view())
然而,当请求URL时,在激活部分之前包含了双斜杠(//):
http://127.0.0.1:8000//activate/MjU/brgs37-d7dee487798f432dc9fb24478dae100b
为了解决这个问题,你可以通过移除开头的斜杠来修改URL模式。请按照以下方式更新你的urls.py文件:
re_path(r'^activate/(?P<uid>[\w-]+)/(?P<token>[\w-]+)/$', views.ActivateUser.as_view())
希望这对你有帮助。
英文:
In your urls.py file, you have defined the activation URL pattern as:
re_path(r'^activate/(?P<uid>[\w-]+)/(?P<token>[\w-]+)/$', views.ActivateUser.as_view())
However, when the URL is requested, it includes a double slash (//) before the activate part:
http://127.0.0.1:8000//activate/MjU/brgs37-d7dee487798f432dc9fb24478dae100b
To fix this issue, you can modify the URL pattern by removing the leading slash. Update your urls.py file as follows:
re_path(r'^activate/(?P<uid>[\w-]+)/(?P<token>[\w-]+)/$', views.ActivateUser.as_view())
I hope this helps you
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论