how to save the 'user' object that not used to insert it everytime you need it using django
i want to do these thing:
1.open the login_view
(which is the page user login)
2.write my username and password , and put 'login' button, then submit to the login_submit
view
3.in login_submit
view , if your username and password are right , you will return to the homepage
4.but when i return HttpResponseRedirect("/")
to the hpmeage, the homepage.html
(the following code) does not has a user
object
5.i have to make it like this in login_submit
view:
return render_to_response('homepage.html',{'user':user})
6.that is Cumbersome , i want to use return HttpResponseRedirect("/")
, so should i set a cookies that save the user , or has any other easy way to save user that not used to insert it everytime you need it .
thanks
the homepage.html is :
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }}. Thanks for logging in.</p>
{% else %}
<p>Welcome, new user. Please <a href="/account/login_view">login</a></p>
{% endif %}
the homepage virw is :
def home(request):
return render_to_response('homepage.html')
this is login_view :
def login_view(request):
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
# Process the data in form.cleaned_data
# ...
return HttpResponseRedirect('/') # Redirect after POST
else:
form = LoginForm() # An unbound form
return render_to_response('accounts/login_view.html',{'form': form,})
the login_view.html is :
<form action="/account/login_submit/" method="post">
<div class="f开发者_开发技巧ieldWrapper">
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
<p><input type="submit" value="login" /> or <a href="/account/register_view"><input type="button" value="register"></a></p>
</form>
and
def login_submit(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect("/")
else:
return HttpResponse('user is not active')
else:
#return HttpResponseRedirect("/account/login_submit")
return HttpResponse('login error')
Context processors.
精彩评论