Python Web Framework Django Learning (iii)

Source: Internet
Author: User
Tags create directory

Python Web Framework Django Learning (iii)

Directory:

V. Django Life cycle

Vi. CBV and FBV of Django

Seven, dictionary, regular-based URLs

Eight, ORM


=================================================================================================

V. Django Life cycle

=================================================================================================




=================================================================================================

Vi. CBV and FBV of Django

=================================================================================================








=================================================================================================

Seven, dictionary, regular-based URLs

=================================================================================================

1. Pre-preparation

1) Create a project test02

d:\python2.7.13\exercise> django-admin Startproject test02

2) Create an app named App01

D:\python2.7.13\exercise> CD test02

d:\python2.7.13\exercise> python manage.py Startapp app01

3) Open test02 project with Pycha

4) Create directory static and templates under the test02 directory

5) configuration

Configuration project file test02 setting.py file (modified three places)

"will be ' django.middleware.csrf.CsrfViewMiddleware ', this line of comment

# ' Django.middleware.csrf.CsrfViewMiddleware ',

"" In the ' DIRS ': [], plus the location of the templates file

' DIRS ': [Os.path.join (Base_dir, ' templates ')],

At the end of the file, add the static folder to the location

Staticfiles_dirs = (
Os.path.join (Base_dir, ' static '),
)

2. Create a function in the views.py file in the app file App01

"To create an index function

def index (Request):
Return HttpResponse (' INDEX ')

"To Create the login function

DEF login (Request):
if Request.method = = ' GET ':
return render (Request, ' login.html ')
elif Request.method = = ' POST ':
user = Request. Post.get (' user ')
PWD = Request. Post.get (' pwd ')
if user = = ' qiuuuu ' and pwd = = ' 123 ':
Return redirect (' http://doublelinux.blog.51cto.com ')
Else
return render (Request, ' login.html ')
Else
Return redirect ('/index/')
return render (Request, ' login.html ')

3. Create an HTML file in the templates directory in the project file test01

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>Title</title>
<body>
<form action= "/login/" method= "POST" >
<p>
<input type= "text" name= "user" placeholder= "username"
</p>
<p>
<input type= "password" name= "pwd" placeholder= "password"
</p>
<p>
<input type= "Submit" value= "Submission"
</p>
</form>
</body>

4, in the project test01 under the urls.py to add the corresponding relationship

From Django.conf.urls import URL
From Django.contrib Import admin
From APP01 Import views

Urlpatterns = [
URL (r ' ^admin/', admin.site.urls),
URL (r ' ^index ', Views.index),
URL (r ' ^login ', Views.login),
]

to this, as long as the user name qiuuuu, password 123 will be able to jump to doublelinux.blog.51cto.com. There is no information related to database storage users.


5, create a dictionary in App01 file views.py, and implement the Index.html page to read the data in the dictionary----to elicit the URL.

1) The views.py file in App01 is modified to:

From django.shortcuts import Render,httpresponse
From django.shortcuts import HttpResponse
From django.shortcuts Import redirect

User_dict = {
' 1 ': {' name ': ' root1 ', ' email ': ' [email protected] ',
' 2 ': {' name ': ' Root2 ', ' email ': ' [email protected] ',
' 3 ': {' name ': ' root3 ', ' email ': ' [email protected] ',
' 4 ': {' name ': ' Root4 ', ' email ': ' [email protected] ',
' 5 ': {' name ': ' root5 ', ' email ': ' [email protected] ',
}


def index (request):
return render (Request, ' index.html ', {' user_dict ': user_dict})

DEF login (Request):
if Request.method = = ' GET ':
return render (Request, ' login.html ')
elif Request.method = = ' POST ':
user = Request. Post.get (' user ')
PWD = Request. Post.get (' pwd ')
if user = = ' qiuuuu ' and pwd = = ' 123 ':
Return redirect (' http://doublelinux.blog.51cto.com ')
Else
return render (Request, ' login.html ')
Else
Return redirect ('/index/')
return render (Request, ' login.html ')


2) Create an HTML file index.html in the directory templates, as follows:

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>Title</title>
<body>
<ul>
{% for k,row in user_dict.items%}
<li>{{k}}----{{row}}</li>
{% ENDFOR%}
</ul>
</body>

3) Modify the urls.py in the project file test02, as follows:

From Django.conf.urls import URL
From Django.contrib Import admin
From APP01 Import views

Urlpatterns = [
URL (r ' ^admin/', admin.site.urls),
URL (r ' ^index ', views.index),
URL (r ' ^login ', Views.login),
]

4) The effect of browser access is:

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/07/FF/wKiom1nYzrKRVcjfAABXNLRGYjI169.png-wh_500x0-wm_ 3-wmp_4-s_4196550903.png "title=" 11111.png "alt=" Wkiom1nyzrkrvcjfaabxnlrgyji169.png-wh_50 "/>

here, a field is created early in the background, and the content in the dictionary is read in the HTML by looping through the front end.

The following will be implemented by clicking on the user to view the user's details.


6, modify the HTML file, modified to:

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>Title</title>
<body>
<ul>
{% for row in user_dict.values%}
<li>
<a href= "http://www.baidu.com" >{{row}}</a>
</li>
{% ENDFOR%}
</ul>
<ul>
{% for k in user_dict%}
<li>
<a href= "http://www.baidu.com" >{{K}}</a>
</li>
{% ENDFOR%}
</ul>
<ul>
{% for row in user_dict.items%}
<li>
<a href= "http://www.baidu.com" >{{row}}</a>
</li>
{% ENDFOR%}
</ul>
</body>

7, modify the APP01 views.py file, modified to:


8.









=================================================================================================

Eight, Create login page, implement user interaction, manage users in the background (database mode)

=================================================================================================


















This article from "Doublelinux" blog, declined reprint!

Python Web Framework Django Learning (iii)

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.