Home > Enterprise >  How to Use URL dispatcher to get clickable link to list contents of my page DJANGO
How to Use URL dispatcher to get clickable link to list contents of my page DJANGO

Time:12-02

I want to make a list of items showing on my index.html page to be clickable, I used the url dynamic reverse technique but i keep getting this error.

Please guide me on what I may be doing wrong.

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/{%25 url%20'entrypage' entry%20%25
Using the URLconf defined in wiki.urls, Django tried these URL patterns, in this order:

admin/
[name='index']
wiki/<str:title> [name='entrypage']
The current path, {% url 'entrypage' entry %, didn’t match any of these.

URLS.PY

app_name = "encyclopedia"

urlpatterns = [
    path("", views.index, name="index"),
    path("wiki/<str:title>", views.entry_page, name='entrypage'),
]

HTML

<ul>
    {% for entry in entries %}
        <li>
            <a href="{% url 'entrypage' entry %">{{ entry }}</a>
        </li>
    {% endfor %}
</ul>

VIEWS.PY

def entry_page(request, title):
    title = util.get_entry(title)
    if title:
        content = markdown2.markdown(title)
        context = {
            "title": title,
            "content": content,
        }

       return render(request, "encyclopedia/entrypage.html", context) 
    else:
       return render(request, "encyclopedia/errorpage.html")

What am i doing wrong please. I'm a beginner

CodePudding user response:

Whoops, you're missing the closing curly bracket of the url template tag

<a href="{% url 'entrypage' entry %}">{{ entry }}</a>

You may also need to pass through the title rather than the entry instance because that's what entry_page uses for the look up.

<a href="{% url 'entrypage' entry.title %}">{{ entry }}</a>
  • Related