Reputation: 11038
urls.py
from django.conf.urls.defaults import patterns, include, url
import myproject.views
urlpatterns = patterns('', (r'^$', myproject.views.home), (r'^login$', apolla.views.login))
views.py
import django.http
import django.template
import django.shortcuts
def home(request):
return django.http.HttpResponse("Welcome home!")
def login(request):
un = request.POST.get('username')
pa = request.POST.get('password')
di = {'unam': un, 'pass': pa}
if un and pa:
di['act'] = "/"
else:
di['act'] = "/login"
return django.shortcuts.render_to_response('login.html', di,
context_instance=django.template.RequestContext(request))
# Why does this code not send me immediately to "/" with
# username and password filled in?
login.html
<html>
<head>
</head>
<body>
<form name="input" method="post" action="{{ act }}">
{% csrf_token %}
Username:
<input type="text" name="username"><br>
Password:
<input type="password" name="password"><br>
<input id="su" type="submit" value="Submit"><br>
</form>
</body>
</html>
When I run the development server and go to localhost:8000/login
and fill in a username
and password
and push the submit button I am not sent to localhost:8000/
as I expected from my login function in views.py
, I just return to localhost:8000/login
. But when I fill in any field and submit for the second time I get directed to localhost:8000
.
I also used print un
and print pa
to see if the post caught the data from the username
and password
fields and it did from the first time, so why am I not being directed to localhost:8000/login
from the first submit with both username
and password
fields filled in?
Upvotes: 0
Views: 261
Reputation: 27861
You can add redirects to your view by:
from django.http import HttpResponseRedirect
def foo_view(request):
# ...
return HttpResponseRedirect('/')
Upvotes: 3