【问题标题】:Using the URLconf defined,Django tried these URL patterns使用定义的 URLconf,Django 尝试了这些 URL 模式
【发布时间】:2020-09-01 08:01:25
【问题描述】:

我正在学习“电影租赁”教程,但出现错误。 使用 vidly.urls 中定义的 URLconf,Django 按以下顺序尝试了这些 URL 模式:

 Using the URLconf defined , Django tried these URL patterns, in this order:
1.admin/
2.movies/ [name='movie_index']
3.movies/ <int:movie_id [name='movie_detail']
The current path, movies/1, didn't match any of these.

我的代码是(来自主 urls.py):

from django.contrib import admin
from django.urls import path, include


urlpatterns = [
    path('admin/', admin.site.urls),
    path('movies/', include('movies.urls'))
]

来自我的网站,即电影(urls.py)

from . import views
from django.urls import path

urlpatterns = [
    path('', views.index, name='movie_index'),
    path('<int:movie_id', views.detail, name='movie_detail')

]

来自views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Movie

def index(request):
    movies = Movie.objects.all()
    return render(request, 'movies/index.html', {'movies': movies})

def detail(request, movie_id):
    return HttpResponse(movie_id)

我做错了什么?

【问题讨论】:

    标签: python django url-parameters


    【解决方案1】:

    在您的movies.urls 中您忘记关闭 URL 参数。

    path('<int:movie_id>/', views.detail, name='movie_detail')
    

    相反,你做了,

    path('<int:movie_id', views.detail, name='movie_detail')
    

    【讨论】:

      【解决方案2】:

      你错过了一个结束&gt;/

      path('<int:movie_id>/', views.detail, name='movie_detail')
      

      【讨论】:

        猜你喜欢
        • 2021-02-20
        • 2018-03-23
        • 2021-05-11
        • 2018-06-28
        • 2017-05-27
        • 2017-06-26
        • 2020-05-05
        • 1970-01-01
        • 2021-04-13
        相关资源
        最近更新 更多