繁体   English   中英

使用定义的 URLconf,Django 尝试了这些 URL 模式

[英]Using the URLconf defined,Django tried these URL patterns

我正在做教程“电影租赁”,但我遇到了错误。 使用 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'))
]

来自 mysite 即电影(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)

我做错了什么?

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

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

相反,你做到了,

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

你错过了一个结束>/

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM