I am new to Django and have started working on a mature Django project.
I want to add a new page to the user account screens, and link to it. But I am getting a NoReverseMatch error.
To /users/urls.py I added:
url(r'^panel/history$',
'theproject.users.views.history',
name='account开发者_StackOverflow社区_history'
),
...which is consistent with the line above it:
url(r'^panel$',
'theproject.users.views.control_panel',
name='account_panel'
),
To /users/views.py I added:
@login_required
def history(request):
return render_to_response('users/ourbrand_history.html', {},
context_instance=RequestContext(request))
...which is consistent with the line above it:
@login_required
def control_panel(request):
return render_to_response('users/ourbrand_panel.html', {},
context_instance=RequestContext(request))
To /templates/users/ourbrand_panel.html I added:
<a href="{% url account_history %}">History</a>
...which is consistent with the line above it:
<a href="{% url account_panel %}">Home</a>
Now when I load /panel I get a TemplateSyntaxError: NoReverseMatch.
Reverse for 'account_history' with arguments '()' and keyword arguments '{}' not found.
UPDATE: As a test, I removed my link in the template -- and /panel loads fine. If I then delete /users/urls.py and /users/views.py /panel still loads. I have deleted all cookies, history etc. Do I have to run a command at the terminal (like rake in ruby) to commit changes to urls.py? Or should changes 'just work'?
Try:
{% url users.views.control_panel %}
{% url users.views.history %}
in the template and change:
url(r'^panel/history/$','theproject.users.views.history',name='account_history'),
url(r'^panel/$','theproject.users.views.control_panel',name='account_panel'),
^
in the urls. (notice the trailing slashes in the first argument.)
If you're using mod_wsgi, then a simple:
$ touch /path/to/your/wsgi_file.wsgi
Should do the trick (without having to restart the server).
Move this:
url(r'^panel/history/$',
'theproject.users.views.history',
name='account_history'
),
from the users/urls.py
to the project level urls.py
.
It could be that the users/urls.py
is not linked to from the main urls.py
that also defines a view for /panel/
Answering my own question (based on help provided here)...
Since the server is production, changes to urls.py
do not have any effect until the server is restarted. Currently I do not have su
access so cannot apachectl restart
but it appears this will resolve the issue.
精彩评论