Django,URL 和未找到视图
Django, URL and Views not found
我正在使用 Django 2.1 并测试 views.py 和 urls.py
我不明白的是为什么每当我输入 URL http://127.0.0.1:8000/blog/post_list 我都会收到 404 错误消息
我的顶urls.py:
from django.urls import path
from django.contrib import admin
from django.conf.urls import include, url
from organizer import urls as organizer_urls
from blog import urls as blog_urls
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(organizer_urls)),
path('tag/', include(organizer_urls)),
path('startup/', include(organizer_urls)),
path('blog/', include(blog_urls))
]
我的应用程序的 urls.py
from django.urls import path
from blog.views import post_list, post_detail
urlpatterns = [
path('',
post_list,
name='blog_post_list'),
path(
'<int:year>/<int:month>/<slug:slug>',
post_detail,
name='blog_post_detail'),
]
我的应用程序的 views.py:
from django.shortcuts import render, get_object_or_404
from .models import Post
# Create your views here.
def post_list(request):
return render(
request,
'blog/post_list.html',
{'post_list':Post.object.all()}
)
def post_detail(request, year, month, slug):
post = get_object_or_404(
Post,
pub_date__year=year,
pub_date__month=month,
slug=slug)
return render(
request,
'blog/post_detail.html',
{'post': post})
错误信息是:
Using the URLconf defined in suorganizer_project.urls, Django tried
these URL patterns, in this order:
admin/ tag/tag_list [name='organizer_tag_list'] tag//
startup/startup_list [name='organizer_startup_list'] startup//
[name='organizer_startup_detail'] tag/ startup/ blog/ The empty path
didn't match any of these.
为什么? url 在那里:
path('',
post_list,
name='blog_post_list'),
这会带我到 post_list 观看次数:
def post_list(request):
return render(
request,
'blog/post_list.html',
{'post_list':Post.object.all()}
)
和 returns 对 post 中所有对象的查询 --Post.object.all()-- ?
我不明白我错过了什么,非常感谢您的帮助! :)
post_list
不在您的 URL 模式中,但它是您的函数名称。如果您以这种方式访问 URL:127.0.0.1:8000/blog/
它将起作用并根据此模式调用 post_list()
视图
path('',
post_list,
name='blog_post_list'),
如果您想让 URL 正常工作,只需按以下方式编辑您的模式,添加 post_list
字符串。
path('post_list',
post_list,
name='blog_post_list'),
我正在使用 Django 2.1 并测试 views.py 和 urls.py 我不明白的是为什么每当我输入 URL http://127.0.0.1:8000/blog/post_list 我都会收到 404 错误消息
我的顶urls.py:
from django.urls import path
from django.contrib import admin
from django.conf.urls import include, url
from organizer import urls as organizer_urls
from blog import urls as blog_urls
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(organizer_urls)),
path('tag/', include(organizer_urls)),
path('startup/', include(organizer_urls)),
path('blog/', include(blog_urls))
]
我的应用程序的 urls.py
from django.urls import path
from blog.views import post_list, post_detail
urlpatterns = [
path('',
post_list,
name='blog_post_list'),
path(
'<int:year>/<int:month>/<slug:slug>',
post_detail,
name='blog_post_detail'),
]
我的应用程序的 views.py:
from django.shortcuts import render, get_object_or_404
from .models import Post
# Create your views here.
def post_list(request):
return render(
request,
'blog/post_list.html',
{'post_list':Post.object.all()}
)
def post_detail(request, year, month, slug):
post = get_object_or_404(
Post,
pub_date__year=year,
pub_date__month=month,
slug=slug)
return render(
request,
'blog/post_detail.html',
{'post': post})
错误信息是:
Using the URLconf defined in suorganizer_project.urls, Django tried these URL patterns, in this order:
admin/ tag/tag_list [name='organizer_tag_list'] tag// startup/startup_list [name='organizer_startup_list'] startup// [name='organizer_startup_detail'] tag/ startup/ blog/ The empty path didn't match any of these.
为什么? url 在那里:
path('',
post_list,
name='blog_post_list'),
这会带我到 post_list 观看次数:
def post_list(request):
return render(
request,
'blog/post_list.html',
{'post_list':Post.object.all()}
)
和 returns 对 post 中所有对象的查询 --Post.object.all()-- ?
我不明白我错过了什么,非常感谢您的帮助! :)
post_list
不在您的 URL 模式中,但它是您的函数名称。如果您以这种方式访问 URL:127.0.0.1:8000/blog/
它将起作用并根据此模式调用 post_list()
视图
path('',
post_list,
name='blog_post_list'),
如果您想让 URL 正常工作,只需按以下方式编辑您的模式,添加 post_list
字符串。
path('post_list',
post_list,
name='blog_post_list'),