Python develops "Django": Routing Rules

Source: Internet
Author: User

Django Request life cycle:

-->url (matching), view function, return user string

-->url (matching), view function, open an HTML file, read the content

Create a Django Project

Django-admin Startproject MySite

CD MySite

Python manage.py Startapp CMDB

MySite

MySite

--Configuration file

-url.py

-settings.py

CD MySite

Cmdb

-views.py

-admin.py

-models.py # Creating a database table

3. Configuration

Template path

Static file path

Note #csrf

4. Writing Procedures

A.url.py Write Correspondence Relation/index/-func

B. views.py

def func (Request): # request contains all the requested data

... Business code

Return HttpResponse (' string ')

return render (Request, ' index.html ', {'})

Return redirect (' URL ')

 C. Template language

{% for item in%}

One, routing system, URL

Second, the View

Request. GET

Request. POST

Request. FILES

#checkbox等多选的内容 request. Post.getlist ()

#上传文件

obj = Request. Files.get (' name ')

Obj.name

f = open (obj.name,mode= ' WB ')

For item in Obj.chunks ():

F.write (item)

F.close ()

views.py

From django.shortcuts import render,httpresponse,redirect# Create your views Here.def index (Request): Return Httprespon Se (' Index ') ' Def login (Request): if Request.method = = ' GET ': Return render (Request, ' login.html ') elif reques T.method = = ' POST ': u = Request. Post.get (' user ') p = Request.  Post.get (' pwd ') if u = = ' Alex ' and p = = ' 123 ': Return redirect ('/index/') Else:return Redirect (' login.html ') Else: # put,delete,head,option ... return redirect ('/index/') ' Def login (Request) : if Request.method = = ' GET ': Return render (Request, ' login.html ') elif Request.method = = ' POST ': v = r Equest. Post.get (' Gender ') print (v) f = Request. Post.getlist (' favor ') # Gets the multi-person value of the checkbox print (f) FFF = Request. Files.get (' file ') # default only gets the file name content = Request. Files.get (' file ') # Fetch file contents print (Fff,type (FFF)) return render (Request, ' login.html ') Else: # Put,del EtE,head,option. Return redirect ('/index/') 
#上传文件的话, form label to do special treatment enctype= "Multipart/form-data"

<! DOCTYPE html>

Login.html

  

Django Routing Rules

1, the URL based on the regular

Create index.html, detail.html files under the Templates directory

<! DOCTYPE html>"en">"UTF-8"> <title>Title</title>{% forK,iteminchUser_dict.items%}    <li><a target="_blank"href="/detail-{{k}}.html">{{item.name}}</a></li>{% ENDFOR%}</body>index.html
index.html
<! DOCTYPE html>"en">"  UTF-8">    <title>Title</title>detail.html
detail.html

Add the corresponding path in the urls.py file

From django.conf.urls import url,includefrom django.contrib import adminfrom CMDB import views urlpatterns = [    url (r ') ^index ', views.index),    url (r ' ^detail-(\d+). html/', Views.detail),]

  

Python development "Django": Routing rules

Related Article

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.