Reputation: 87
OK, I have two different views, both in the project site-wide area.
urls.py
url(r'^accounts/login/$', 'taxo.views.login'),
url(r'^accounts/invalid/$', 'taxo.views.invalid'),
...
taxo/views.py
def login(request):
c = {}
c.update(csrf(request))
return render_to_response('login.html', c)
def invalid(request):
return render_to_response('invalid.html',{'title':'invalid'})
templates/login.html
<form action="/accounts/auth/" method="post">{% csrf_token %}
<label for="username">User name</label>
<input type="text" name="username" value="" id="username">
<label for="password">Password</label>
<input type="password" name="password" value="" id="password">
<input type="submit" value="login" />
</form>
templates/invalid.html
<form style="float: right" action="accounts/login/" method="post">
{% csrf_token %}
{{form}}
<input type="submit" value="Login" class="search"/>
</form>
With the above code, I got Page not Found error
Page not found (404)
Request Method: POST
Request URL: http://127.0.0.1:8000/accounts/invalid/accounts/login/
Django parses the requested url as relative to the url of the current page. When I replaced the action with the {% url %}
tag. I got a NoReverseMatch at /accounts/invalid/ error
How do I do this correctly?
Upvotes: 1
Views: 120
Reputation: 408
And here's the reason:
Request URL: http://127.0.0.1:8000/accounts/invalid/accounts/login/
$ at the end of regex means nothing's after slash:
url(r'^accounts/login/$', 'taxo.views.login', name='login'),
url(r'^accounts/invalid/$', 'taxo.views.invalid', name='invalid'),
therefore you may use those urls:
http://127.0.0.1:8000/accounts/login/
http://127.0.0.1:8000/accounts/invalid/
edit: why one of your URLs in template redirects begins with slash and one without? Try this one:
<form style="float: right" action="{% url 'login' %}" method="post">
Upvotes: 0
Reputation: 1073
Try this:
<form style="float: right" action="/accounts/login/" method="post">
{% csrf_token %}
{{form}}
<input type="submit" value="Login" class="search"/>
</form>
Upvotes: 1