skip to Main Content

Im trying to make a wikipedia clone for studying purposes, ‘encyclopedia’ is my django app name and {{entry}} its a wiki page. My code line looks like this : <a href="{% url 'encyclopedia:{{entry}}'%}">,
I searched in google but cant find anyone talking about django variables inside HTML tag atributtes.

When I run my code i see this error:

`NoReverseMatch at /
Reverse for '{{entry}}' not found. '{{entry}}' is not a valid view function or pattern name. `

2

Answers


  1. The issue you’re facing is because you’re using Django template variables inside the {% url %} template tag incorrectly. In Django templates, you don’t need to use double curly braces ({{ }}) inside the {% url %} tag.

    Here’s how you should structure your link:

    <a href="{% url 'encyclopedia:entry' entry=entry %}">
    
    Login or Signup to reply.
  2. You cannot directly use Django template variables inside the {% url %} template tag. Instead, you need to pass the variable as an argument to the template tag itself. Here’s the corrected code:

        <a href="{% url 'encyclopedia:entry' entry=entry %}">
    

    Make sure that the entry variable is properly defined and passed to the template context from the view for this code to work correctly.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search