Django的URL没有命中正确的端点。

huangapple go评论62阅读模式
英文:

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, &#39;index.html&#39;)


class ActivateUser(GenericAPIView):

    def get(self, request, uid, token, format = None):
        payload = {&#39;uid&#39;: uid, &#39;token&#39;: token}

        url = &quot;http://localhost:8000/api/auth/users/activation/&quot;
        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(&#39;admin/&#39;, admin.site.urls),
    path(&#39;api/&#39;, include(&#39;account.urls&#39;)),
    path(&#39;&#39;, views.index, name=&#39;index&#39;),
    re_path(r&#39;^activate/(?P&lt;uid&gt;[\w-]+)/(?P&lt;token&gt;[\w-]+)/$&#39;, 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&#39;^activate/(?P&lt;uid&gt;[\w-]+)/(?P&lt;token&gt;[\w-]+)/$&#39;, 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&#39;^activate/(?P&lt;uid&gt;[\w-]+)/(?P&lt;token&gt;[\w-]+)/$&#39;, views.ActivateUser.as_view())

I hope this helps you

huangapple
  • 本文由 发表于 2023年7月17日 12:28:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定