Recommand · June 15, 2021 0

Can't add an anchor tag using a url path to another Django template

I am trying to add an anchor tag using a url in Django like below:

layout.html

{% extends "tasks/layout.html" %}
{% block body %}
    <h1>Tasks</h1>
    <ul>
        {% for task in tasks %}
            <li>{{ task }}</li>
        {% endfor %}
    </ul>
    <a href="{% url 'add' %}">Add Tasks</a>
{% endblock %}

Views.py

from django.shortcuts import render

tasks = ["foo", "bar", "baz"]

# Create your views here.
def index(request):
    return render(request, "tasks/index.html", {
        "tasks": tasks
    })

def add(request):
    return render(request, "tasks/add.html")

urls.py

from django.urls import path

from . import views

app_name = "tasks"
urlpatterns = [
    path("", views.index, name="index"),
    path("add", views.add, name="add")
]

However, I keep getting the following error when setting the href using the url method, but not when I hardcode the path:

Error during template rendering
In template C:\Users\User\Desktop\Coding\CS50W\Week3-Django\lecture3\tasks\templates\tasks\layout.html, error at line 0
'set' object is not reversible
1 <!DOCTYPE html>
2 <html lang="en">
3     <head>
4         <title>Tasks</title>
5     </head>
6     <body>
7         {% block body %}
8         {% endblock %}
9     </body>
10 </html>
Traceback Switch to copy-and-paste view
C:\Users\User\Desktop\Coding\CS50W\Week3-Django\venv\lib\site-packages\django\core\handlers\exception.py, line 47, in inner
                response = get_response(request) …
▶ Local vars
C:\Users\User\Desktop\Coding\CS50W\Week3-Django\venv\lib\site-packages\django\core\handlers\base.py, line 181, in _get_response
                response = wrapped_callback(request, *callback_args, **callback_kwargs) …
▶ Local vars
C:\Users\User\Desktop\Coding\CS50W\Week3-Django\lecture3\tasks\views.py, line 6, in index
    return render(request, "tasks/index.html", { …
▶ Local vars

Any pointers to what I could be doing wrong?