0
我使用Django 1.8,並且我想在網址中使用slu bu創建博客。但我的代碼不起作用。django中與CBV的網址S 1.8 1.8
這裏是我的聯繫張貼詳細信息模板:
{% extends "base.html" %}
{% block head_title %}<title>Blog</title>{% endblock %}
{% block content %}
<div class="container">
<h2>Blog</h2>
{% for i in blog %}
<p><b>{{ i.date|date:"D, d M Y" }}</b></p>
<h4><a href="{% url 'projde:blogdetail' slug=i.slug %}">{{ i.title }}</a></h4>
<p>{{ i.text|truncatewords:100 }}</p>
{% if not forloop.last %}
<hr>
{% endif %}
{% endfor %}
</div>
{% endblock %}
這裏是我的模型:
class BlogPost(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField(max_length=200, unique=True)
text = models.TextField()
date = models.DateTimeField()
is_online = models.BooleanField(default=False)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("blogdetail", kwargs={"slug": self.slug})
這裏是我在我的應用程序的所有觀點,但在這種情況下,最重要的是最後一個。
class Home(TemplateView):
template_name = "projde/index.html"
class Projects(ListView):
template_name = "projde/projects.html"
context_object_name = "all_projects"
model = ProjectItem
def get_queryset(self):
return ProjectItem.objects.filter(is_online=True)
class Resume(ListView):
template_name = 'projde/resume.html'
context_object_name = 'resume'
model = ResumeItem
def get_queryset(self):
return ResumeItem.objects.filter(is_online=True)
class Blog(ListView):
template_name = "projde/blog.html"
context_object_name = "blog"
model = BlogPost
def get_queryset(self):
s = BlogPost.objects.all().order_by("-date")
return s
class BlogDetail(DetailView):
model = BlogPost
template_name = "projde/blogdetail.html"
和我的網址:
urlpatterns = [
url(r'^$', Home.as_view(), name="home"),
url(r'^projects/$', Projects.as_view(), name="projects"),
url(r'^resume/$', Resume.as_view(), name="resume"),
url(r'^blog/$', Blog.as_view(), name="blog"),
url(r'^blog/(?P<slug>\S+)$', BlogDetail.as_view(), name="blogdetail"),
]
感謝您的幫助,但我得到一個錯誤:**反向'blogdetail'參數'()'和關鍵字參數'{'slug':''}'找不到。嘗試1種模式:['blog /(?P \\ S +)$'] ** –
mark
完成後,請檢查。 – mark
哪個網址導致錯誤?你在問題中顯示了哪個模板? – Alasdair