Home > database >  how to pass in slug url in django template
how to pass in slug url in django template

Time:02-18

i want to pass in a slug in my django template so i can be able to add a post as favourite but i do not know the right way to pass in the slug this is what i have done href="{% url 'elements:favourite' elements_slug.slug %}

<a href="{% url 'elements:favourite' elements_slug.slug %}" >Favourite</a>

views.py

def favourite(request, elements_slug):
    user = request.user
    elements = Elements.objects.get(slug=elements_slug)
    profile = Profile.objects.get(user=user)

    if profile.favourite.filter(slug=elements_slug).exists():
        profile.favourite.remove(elements)
    else:
        profile.favourite.add(elements)
    return HttpResponseRedirect(reverse('elements:vector-details', args=[elements_slug]))

UPDATED WITH ERROR error

Reverse for 'favourite' with arguments '('',)' not found. 1 pattern(s) tried: ['elements/(?P<elements_slug>[-a-zA-Z0-9_] )/favourite$']

CodePudding user response:

Try it

<a href="{% url 'favourite' slug=instance.slug %}">Favourite</a>
  • Related