【问题标题】:Slug in url with CBV in django 1.8在 django 1.8 中使用 CBV 插入 url
【发布时间】:2016-11-08 21:10:06
【问题描述】:

我使用 Django 1.8,我想在 url 中使用 slug 构建博客。但是我的代码不起作用。

这是我的模板,带有帖子详细信息的链接:

{% 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"),
]

【问题讨论】:

    标签: python regex django url django-class-based-views


    【解决方案1】:

    ListView 模板中,如果您未设置context_object_name,则博客文章列表将显示为blogpost_list

    {% for blogpost in blogpost_list %}
    <p><b>{{ blogpost.date|date:"D, d M Y" }}</b></p>
    <h4><a href="{% url 'projde:blogdetail' slug=blogpost.slug %}">{{ blogpost.title }}</a></h4>
    {% endfor %}
    

    既然你已经为你的列表视图设置了context_object_name = 'blog',你应该把上面的for循环改成{% for blogpost in blogs %}

    如果您仍然收到错误'{'slug': ''}',则表明您的数据库中有一个带有slug='' 的博文。通过 shell 或 Django 管理员修复此问题,然后刷新页面。

    DetailView 模板中,不需要for 循环,可以使用{{ blogpost }} 访问博文。

    【讨论】:

    • 感谢您的帮助,但我收到一个错误:Reverse for 'blogdetail' with arguments '()' 和关键字参数 '{'slug': ''}' not found。尝试了 1 种模式:['blog/(?P\\S+)$']
    • 完成,请检查。
    • 哪个 URL 导致了错误?您在问题中显示的是哪个模板?
    • 此网址引发错误:127.0.0.1:8000/blog 此网址的模板位于问题之上
    • 谢谢大家! :)
    猜你喜欢
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    • 2016-02-21
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多