Python Django中出错:Django-3使用Django_test.URL中定义的URLconf,按以下顺序尝试了这些URL模式:

Python Django中出错:Django-3使用Django_test.URL中定义的URLconf,按以下顺序尝试了这些URL模式:,python,django,rest,django-rest-framework,Python,Django,Rest,Django Rest Framework,我曾尝试在这个项目中设置这些URL,但在尝试访问registration.html文件和与REST api相关的api auth URL时,我得到了404 供参考: 索引=应用程序 django_试验=现场 index/url.py from django.urls import include, path from rest_framework import routers from . import views router = routers.DefaultRouter() rout

我曾尝试在这个项目中设置这些URL,但在尝试访问registration.html文件和与REST api相关的api auth URL时,我得到了404

供参考:

  • 索引=应用程序
  • django_试验=现场
  • index/url.py

    from django.urls import include, path
    from rest_framework import routers
    from . import views
    
    router = routers.DefaultRouter()
    router.register(r'users', views.UserViewSet)
    
    # Wire up our API using automatic URL routing.
    # Additionally, we include login URLs for the browsable API.
    urlpatterns = [
        path('', views.homepage, name="homepage"),
        path('/register', views.registration, name="registration"),
        path('api', include(router.urls)),
        path('api-auth/', include('rest_framework.urls', namespace="rest framework")),
    ]
    
    django_测试/url.py

    from django.contrib import admin
    from django.urls import path, include
    
    urlpatterns = [
        path('', include('index.urls')),
        path('admin/', admin.site.urls)
    ]
    
    index/views.py

    from django.shortcuts import render
    def homepage(request):
        return render(request,"home/homepage.html")
    
    def registration(request):
        return render(request, 'home/registration.html')
    

    我们所有的HTML文件都位于index/templates/home目录中

    您可以尝试将name=“registration”更改为name=“register”

    它对注册表有效,那么api身份验证呢?好的,请尝试将路径('api-auth/',include('rest_framework.url',namespace=“rest framework')中的'api auth/'更改为'/api auth/',include('rest),刚刚尝试过,仍然会出现404错误好的,我最后的建议是将其更改为“/api auth”,所以只需删除末尾的斜杠:D我希望它有帮助!!