找不到 Django 页面
Django Page not found
我想创建 url 以转到这样的链接:示例。com/baiviet/post-example/
- *post-例子是鼻涕虫
这是我的根 urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', include('blog.urls')),
)
那么,这是我的 blog/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from blog import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^baiviet/(?P<slug>)/$', views.view_post, name='viewpost'),
)
我的views.py:
def view_post(request, slug):
getpost = get_object_or_404(Blog, slug=slug)
return render(request, 'view_post.html', {'post':getpost})
还有我的 view_post.html:
{{ post.content }}
我唯一的问题是 "Page Not Found" 错误。我试图解决它,但 post 我花了 2 个小时才解决这个问题。我希望有人能帮我解决这个问题。谢谢你
您为 url:
配置了一个 空捕获组
url(r'^baiviet/(?P<slug>)/$', views.view_post, name='viewpost')
HERE ^
您需要提供 slug 匹配的模式,例如字母数字、下划线和破折号:
url(r'^baiviet/(?P<slug>[a-zA-Z0-9_-]+)/$', views.view_post, name='viewpost')
404 的原因是在您的根 urlconf
中,您有
url(r'^$', include('blog.urls'))
这里,$
表示url模式结束。将其更改为
url(r'^/', include('blog.urls'))
# ^ note the $ shoudl be replaced by / when you are doing an include.
Note that the regular expressions in this example don’t have a $ (end-of-string match character) but do include a trailing slash. Whenever Django encounters include() (django.conf.urls.include()), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
缺少模式的问题,正如 alecxe 在 <slug>
上提到的那样,在 解决了这个问题 (404) 之后会出现。
编辑:
要访问主页,您需要有尾随 /
或将设置 APPEND_SLASH
设置为 True
。由于您的 URL 模式需要前缀 /
- 现在,如果您不想要它,请在您的根目录 urlconf 中,将 r'^/'
更改为 r'^'
我想创建 url 以转到这样的链接:示例。com/baiviet/post-example/
- *post-例子是鼻涕虫
这是我的根 urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', include('blog.urls')),
)
那么,这是我的 blog/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from blog import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^baiviet/(?P<slug>)/$', views.view_post, name='viewpost'),
)
我的views.py:
def view_post(request, slug):
getpost = get_object_or_404(Blog, slug=slug)
return render(request, 'view_post.html', {'post':getpost})
还有我的 view_post.html:
{{ post.content }}
我唯一的问题是 "Page Not Found" 错误。我试图解决它,但 post 我花了 2 个小时才解决这个问题。我希望有人能帮我解决这个问题。谢谢你
您为 url:
配置了一个 空捕获组url(r'^baiviet/(?P<slug>)/$', views.view_post, name='viewpost')
HERE ^
您需要提供 slug 匹配的模式,例如字母数字、下划线和破折号:
url(r'^baiviet/(?P<slug>[a-zA-Z0-9_-]+)/$', views.view_post, name='viewpost')
404 的原因是在您的根 urlconf
中,您有
url(r'^$', include('blog.urls'))
这里,$
表示url模式结束。将其更改为
url(r'^/', include('blog.urls'))
# ^ note the $ shoudl be replaced by / when you are doing an include.
Note that the regular expressions in this example don’t have a $ (end-of-string match character) but do include a trailing slash. Whenever Django encounters include() (django.conf.urls.include()), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
缺少模式的问题,正如 alecxe 在 <slug>
上提到的那样,在 解决了这个问题 (404) 之后会出现。
编辑:
要访问主页,您需要有尾随 /
或将设置 APPEND_SLASH
设置为 True
。由于您的 URL 模式需要前缀 /
- 现在,如果您不想要它,请在您的根目录 urlconf 中,将 r'^/'
更改为 r'^'