[英]Basic Django,How to write a URLConf for a blog's DetailView Post?
I am new to Django , I have a problem writing the right code for URLConf for my blog app in mysite directory.The blog appears at http://127.0.0.1:8000/blog/ but i could not DetailView the Posts within the blog using a hyperlink. 我是Django的新手,我在为mysql目录中的博客应用程序编写URLConf的正确代码时遇到了问题。博客出现在http://127.0.0.1:8000/blog/但我无法在博客中查看帖子使用超链接。 The error i am getting is "Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
我得到的错误是“使用mysite.urls中定义的URLconf,Django按以下顺序尝试了这些URL模式:
My mysite\\blog\\url.py file looks like this: 我的mysite \\ blog \\ url.py文件如下所示:
from django.conf.urls import patterns, include, url
from django.views.generic import ListView,DetailView
from blog.models import Post
urlpatterns=patterns('',
url(r'^$',ListView.as_view(
queryset=Post.objects.all().order_by("-date")[:10],
template_name="blog.html")),
url(r'^(blog/?P<post_id>\d+)/$', DetailView.as_view(
model = Post,
template_name = "post.html")),
)
How can i display a DetailView for the Posts in my blog app?I have the template files of blog, base and post.html. 如何在我的博客应用程序中显示帖子的DetailView?我有blog,base和post.html的模板文件。 The post.html looks like:
post.html看起来像:
{%extends "base.html"%}
{%block content%}
<h3><a href="/blog/{{post.id}}">{{post.title}}</a></h3>
<div class ="post_meta">
on {{post.date}}
</div>
<div class = "post_body">
{{post.body|safe}}
</div>
{% endblock %}
Your URLConf isn't properly calling the URL in your template. 您的URLConf未正确调用模板中的URL。
url(r'^(blog/?P<post_id>\d+)/$',
...is trying to get a url like www.yoursite.com/blog/(blog/post_id/
with the /(blog
as part of the url. First, you've already included /blog/ when you include yourapp.urls
in the main URLConf. Second, your (
is in the wrong spot. ...正在尝试使用
/(blog
作为网址的一部分)获取网址,例如www.yoursite.com/blog/(blog/post_id/
。首先,当您包含yourapp.urls
时,您已经包含/ blog /在主要的URLConf。第二,你的(
在错误的地方。
Try changing it to: 尝试将其更改为:
url(r'^(?P<post_id>\d+)/$',....rest of your URLConf....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.