Developing Python Django program under Linux (Session read/write)

Source: Internet
Author: User

1. Login to set Session information

defloginsession (req):ifReq.method = ='POST': LoginForm=LoginForm (req. POST)ifloginform.is_valid ():Printloginform.cleaned_data username= loginform.cleaned_data["username"] Password= loginform.cleaned_data["userpwd"] Users=registeruser.objects.filter (username =username, password =password)PrintUsersifusers:req.session['username']=usernamereturnHttpresponseredirect ('/app1/indexsession/')            Else:               returnHttpresponseredirect ('/app1/login/')            Else: LoginForm=LoginForm ()returnRender_to_response ('login.html',{'LoginForm': LoginForm})

Templates page in template

<?xml version="1.0"encoding="UTF-8"?> <! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 strict//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-STRICT.DTD">"http://www.w3.org/1999/xhtml"xml:lang="en"lang="en">"Content-type"Content="text/html; Charset=utf-8"/> <title></title>{%ifIsLogin%}  <li> logged in User: {{sessionloginname}} <a href="/app1/loginoutsession/">Logout</a></li> {%Else%}  <li><a href="/app1/loginsession/">Login</a></li>{%endif%}<div>{% forBookinchBooks%}    {% forAuinchBook.author.all%} Author: {{au}}<br/>  {%endfor%}{% endfor%}</div></body>


2. Get the user information in session

defindexsession (req): SessionLoginName= Req.session.get ('username',"')              Print 'SessionLoginName:'+sessionloginname IsLogin=FalseifSessionloginname:islogin=TrueElse: IsLogin=False aus=Author.objects.all () Books=Book.objects.all () T= Loader.get_template ('index1.html') C=context ({'aus': AUS,'Books': Books,'IsLogin': IsLogin,'SessionLoginName': SessionLoginName}) returnHttpResponse (T.render (c))


3. Delete information from session

def loginoutsession (req):     del req.session['username']    return HttpResponse ('loginout ok!! ')

Developing Python Django program under Linux (Session read/write)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.