【问题标题】:redirect with primary key django {% url %}使用主键重定向 django {% url %}
【发布时间】:2017-04-29 01:18:39
【问题描述】:

我想制作博客,里面有类别和帖子。 应显示类别,当您单击它时,您将被重定向到显示该类别文章的另一个站点。

models.py:

class Category(CMSPlugin):
    title = models.CharField(max_length=20, default='category')

    def __unicode__(self):
        return self.title


class Blog_post(CMSPlugin):
    category = models.ForeignKey(Category)
    style = models.ForeignKey(Blog_style)
    title = models.CharField(max_length=200, default='title')
    description = models.CharField(max_length=200,default='description')
    image = models.ImageField(upload_to='static', null=True, blank=True)
    text = models.TextField()
    created_date = models.DateTimeField(default=timezone.now)
    published_date = models.DateTimeField(blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __unicode__(self):
        return self.title

views.py

def Blog_list(request):
    posts = Blog_post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    category = Category.objects.all()
    return render(request, 'blogspot.html', {'posts': posts, 'category':category})

def post_detail(request, pk):
    post = get_object_or_404(Blog_post, pk=pk)
    return render(request, 'post_detail.html', {'post': post})

def category_detail(request, pk):
    cat = get_object_or_404(Category, id=pk)
    post_with_category = Blog_post.objects.filter(category=cat)
    return render(request, 'articles.html', {'post_with_category': post_with_category})

blogspot.html

{% for post in posts %}
    <h1><a href="{% url 'post_detail' pk=post.pk %}">{{post.title}}</a></h1>
     <a href="{% url 'category_detail' pk=post.category.id %}" >{{ post.category }}</a>
    {{post.title}}
    {{ post.description }}
    {{ post.image }}
    {{ post.text }}{{ post.published_date }}
{% endfor %}

到目前为止一切正常。我可以点击 {{post.title}} 并重定向到 post_detail。现在我想用类别做同样的逻辑。当我点击{{post.category}}时,我想重定向到articles.html,在那里你可以看到特定类别的所有文章。

编辑:

我插入了代码以按类别显示帖子。我坚持使用 for 循环。如果我使用帖子中提到的循环,我会得到所有帖子和类别。问题是如果我在一个类别中有 2 个帖子,并且此循环将在模板中显示 2x“类别”。

所以我编辑了我的 for 循环。

{% for post in category %}
        {{post.title}}
        {% endfor %}

如果我在这个循环中插入&lt;a href="{% url 'category_detail' pk=post.category.id %}" &gt;{{post.title}},我就不会得到反向匹配。 我尝试修改views.py category_detail

网址应该看起来像localhost/&lt;category&gt;/ 另一个问题是,"select*from Table Where Column_id= id ; 的 QRM 替代命令是什么

urls.py

 url(r'^blog/$', views.Blog_list, name='Blog_list'),
    url(r'^blog/(?P<pk>\d+)/$', views.post_detail, name='post_detail'),

【问题讨论】:

标签: django django-templates django-cms


【解决方案1】:

如果我理解您的问题,django 允许您通过主对象引用 FK 对象。因此,由于您的 post.category 是您的 Category 模型的一个实例,您应该能够使用 post.category.id 进行反向查找,因此您的模板将具有以下内容:

<a href="{% url 'category_detail' pk=post.category.id %}" >{{ post.category }}</a>

然后,在您的 category_detail 视图中,您只需使用 pk 进行查找:

cat = get_object_or_404(Category, id=pk)
post_with_category = Blog_post.objects.filter(category=cat)

然后您可以通过新的post_with_category 对象列表在新的 url 链接上显示具有相同类别的帖子列表。

编辑:

您的网址可能需要包含以下内容以使上述 html href 标记起作用:

url(r'^cat/(?P<pk>[0-9]+)/$', views.category_detail, name='category_detail'),

【讨论】:

  • 感谢您的帮助。你能帮我提供网址吗,我得到数字。如何在 url 中包含类别名称?
  • 如果我在 category2 中有 2 个标题,则名称“category”会打印两次,因为是“post in posts”。我该如何解决这个问题?如果我有五个帖子,我会在模板中打印 5 次类别 2
  • 此代码显示与原始帖子具有相同类别的所有帖子,除非您在articles.html 上列出该类别,否则不应列出重复项。从您的类别模型来看,您似乎不支持在一个类别中包含 2 个标题...?
  • 查看类别中帖子的编辑循环,如果我插入 {{ post.category }} 我得到一个没有反向匹配的错误
  • 您是否添加了带有“category_detail”名称的网址?如果 Django 在您的 href 中找不到 url 名称,它将失败反向查找,这就是这里发生的情况。
猜你喜欢
  • 1970-01-01
  • 2015-10-01
  • 2013-02-04
  • 1970-01-01
  • 2019-09-08
  • 2020-09-07
  • 1970-01-01
  • 1970-01-01
  • 2019-01-01
相关资源
最近更新 更多