简体   繁体   English

Django - 带有 2 个参数的 URL 模板标签

[英]Django - URL template tag with 2 arguments

I'm new to Django and sorry if the question is silly.我是 Django 的新手,如果问题很愚蠢,我很抱歉。 I have a URL with two slugs, one for the category which is a manytomany field and one for the posts:我有一个带有两个 slug 的 URL,一个用于属于 manytomany 字段的类别,另一个用于帖子:

path('<slug:slug_staticpage>/<slug:slug>/', views.post, name='post_detail')

views.py视图.py

def post(request, slug, slug_staticpage):
    category = get_object_or_404(StaticPage, slug_staticpage=slug_staticpage)
    blogpost = get_object_or_404(Post, slug=slug)
    post = Post.objects.filter(slug=slug)
    page_id = category.pk
    related_posts = Post.objects.filter(static_page__pk=page_id)[:6]
    return render(request, "blog/blog-post-final.html", {'slug': slug, 'slug_staticpage': category.slug_staticpage, 'post':post, 'related_posts':related_posts})

models.py模型.py

class Post(models.Model):
    title = models.CharField(max_length=300)
    content = RichTextField()
    slug = models.SlugField(max_length=200, unique=True)
    published = models.DateTimeField(verbose_name="Published", default=now())
    image = models.ImageField(verbose_name="Image", upload_to="blog", null=True, blank=True)
    author = models.ForeignKey(User, verbose_name="Author", on_delete=models.PROTECT)
    categories = models.ManyToManyField(Category, verbose_name="Categories", related_name="get_post")
    static_page = models.ManyToManyField(StaticPage, verbose_name="Página estática", related_name="get_post")
    created = models.DateField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.IntegerField(choices=STATUS, default=0)

I want to display those URLs in my homepage via the template tag, but I don't know how to retrieve the slug_staticpage slug:我想通过模板标签在我的主页中显示这些 URL,但我不知道如何检索 slug_staticpage slug:

<a href="{% url 'blog:post_detail'  slug_staticpage=post.slug_staticpage slug=post.slug %}">

The above syntax is not working, I can only retrieve the slug of the post.上面的语法不起作用,我只能检索帖子的slug。

Can someone help me sort this out?有人可以帮我解决这个问题吗? Thanks a lot for your precious help :)非常感谢您的宝贵帮助:)

Askew歪斜

As you have a Many-to-Many relationship, you should have a single url for each of those static_page objects.当你有一个多到多的关系,你应该为每个单个URL static_page对象。

You can achieve that via iterating over the static_page objects:您可以通过迭代static_page对象来实现:

{% for slug_url in post.slug_staticpage.all %}
   <a href="{% url 'blog:post_detail'  slug_staticpage=slug_url slug=post.slug %}"> 
{% endfor %}

although I'm not sure whether you really need such relationship or not.虽然我不确定你是否真的需要这种关系。

I solved it myself by adding the following to models.py我通过将以下内容添加到 models.py 中自己解决了这个问题

def get_absolute_url(self):
   return reverse('blog:post_detail', kwargs={'slug_staticpage':self.static_page.slug_staticpage, 'slug':self.slug})

Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM