While doing the text "python django super introduction" (Shuwa System), I entered the text exactly and an error occurred. There was no description in the errata provided by the publisher, so I posted it for study purposes. In the text I am creating a hello application.
django:3.0.2 python:3.7.4 OS:macOS Mojave 10.14.6
It happened when I was doing P.82 (moving multiple pages) on page 2-2.
index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>{{ title }}</title>
</head>
<body>
<h1>{{ title }}</h1>
<p>{{ msg }}</p>
<p><a href="{% url goto %}">{{ goto }}</a></p>
</body>
</html>
views.py
#Before correction
# index()Only excerpt
def index(request):
params = {
'title': 'Hello/index',
'msg': 'This is a sample page.',
'goto': 'next',
}
return render(request, 'hello/index.html', params)
When accessing index.html in this state, the following error occurred.
NoReverseMatch at /hello/
Reverse for 'next' not found. 'next' is not a valid view function or pattern name.
What is "next"? ?? Is angry. .. ..
From the conclusion, it was due to the way'goto' was written in views.py. After modifying it as follows, it became normal.
views.py
#5th line
#Change before:
'goto': 'next',
#After change:
'goto': 'hello:next',
Execution result screen:
The display of the link part is different from the text, but that is not the essence, so I will not touch it here. (Only "next" is supposed to be displayed in the text) The cause was that the template {% url goto%} part in index.html was not sent in the correct format. The format of the goto part should be'app name: name', but there was an inconsistency because only name came.
The above is not described in the errata of Publisher support page, and I thought that some people would stumble on the same part, so I myself I posted it for the purpose of studying. I'm still studying django, so if you have any questions, please m (_ _) m
Recommended Posts