Django view is rendering 404 page instead of given html template
I'm working on a wiki project with django. I'm trying to render 'add.html' with the view add, but it sends me to 404 instead. All the other views are working fine. How should I fix add?
views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse
from django import forms
import re
from . import util
def index(request):
return render(request, "encyclopedia/index.html", {
"entries": util.list_entries()
})
def detail(request, entry):
#if search based on title returns result
if util.get_entry(entry):
content = util.get_entry(entry)
return render(request, "encyclopedia/details.html",
{
"title": entry,
"content": content
})
else:
return render(request, "encyclopedia/404.html", {
'entry': entry
})
def add(request):
return render(request, "encyclopedia/add.html")
urls.py:
from django.urls import path
from . import views
app_name = "wiki"
urlpatterns = [
path("", views.index, name="index"),
path("<str:entry>/", views.detail, name="entry"),
path("add/", views.add, name="add"),
]
in your urls.py
, <str:entry>/
path is defined before the add/
path.
this causes django to interpret add/
as a dynamic entry parameter for the detail view instead of routing it to the add view.
urlpatterns = [
path("", views.index, name="index"),
path("add/", views.add, name="add"), # place "add/" before "<str:entry>/"
path("<str:entry>/", views.detail, name="entry"),
]