私はDjango 1.8を使用しています。私はurlのスラッグを使ってブログを作成したいと思います。しかし、私のコードは動作しません。ここでDjangoのCBVを使用したURLのスラグ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"
と私のURL:あなたはcontext_object_name
を設定しない場合ListView
テンプレートで
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
どのURLでエラーが発生していますか?あなたの質問にはどのテンプレートが表示されていますか? – Alasdair