Django - URL template tag with 2 arguments

Askew

I'm new to Django and sorry if the question is silly. I have a URL with two slugs, one for the category which is a manytomany field and one for the posts:

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

views.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

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:

<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.

Can someone help me sort this out? Thanks a lot for your precious help :)

Askew

Askew

I solved it myself by adding the following to models.py

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

Cheers!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Types of url arguments when linking via <a> tag in django template

Django template, send two arguments to template tag?

django custom template tag, pass multiple arguments

For the Django template {%url%} tag, how do I find all URL arguments from generic view contexts, not just the last?

how to use `url` template tag in `with` template tag in django templates

Cannot properly generate URL with captured arguments using {% url %} template tag

Django - how to use a template tag within a url

Django url template tag gives NoReverseMatch Error

Django 1.8 template's URL tag TypeError

django template url tag with empty argument

Passing variable through Django template url tag

Pass in dynamic href using Django include template tag AND url tag

Is there a way to pass URL template tag from database to template in Django?

Django template with html tag in jinja2

How to add url parameters to Django template url tag?

How to use Django url template tag with variable as url path

accessing url parameters of django template url tag in createView Class

Pass multiple arguments from Django template for loops into Django template filter or tag

How to add date-time arguments to django URL tag

How to pass a template tag from one template to another in django 2

Django URL with arguments pointing to the same page using "with as template"?

Is it possible to pass query parameters via Django's {% url %} template tag?

I want an exception, if django template tag `static` renders a broken URL

Django: how to correctly pass an URL in include template tag?

Get javascript variable's value in Django url template tag

NoReverseMatch at / error in django after url template tag usage

Why is Django adding "/accounts/logout/" to all {% url %} template tag lookups?

How does Django's `url` template tag reverse a Regex?

template filter tag in django - filtering {{activate_url}}