And routing functions view Django

Routing System

In django, the correspondence relationship between the logical function uri we call routing system

Pseudo-static

Pseudo-static is relative to a static file is, for example, https://www.cnblogs.com/hesujian/p/11165818.html

We call it pseudo-static, because he is actually not a static html page, just disguised as a static html page

Pseudo-static benefits:

  1, appearance (question mark traditional stitching looks messy)

  2, seo (search engine optimization), search engines prefer static pages indexed, so we are made to increase the collection of pseudo-static chance

Achieve pseudo-static routing and distribution

Now that we have used pseudo-static url to access the server, the server how to get the desired value

For example, we have: http: //127.0.0.1: 8000 / up_student / id = 12 & name = hua?

Disguised as: http: //127.0.0.1: 8000 / up_student / 12 / hua /

The background you need to use technology to receive data routing distribution

Route distribution function in the background, and URI matches, the packet through the positive expression, the required value of the packet, and then spread treated

Code:

Copy the code
# 1, directly through the packet received by the traditional values sequentially 
DEF up_student (Request, ID, name): 
    Print (ID, name) 

the urlpatterns = [ 
    URL (R & lt '^ up_student / (\ W +) / (\ W +)', up_student ), 
] 
# 2, we can well-known packet without sequentially can pass value 

DEF up_student (Request, name, ID): 
    Print (ID, name) 

the urlpatterns = [ 
    ? URL (R & lt '^ up_student / (P <ID > \ W +) / (? P <name> \ W +) ', up_student) 
]
Copy the code

Regular route

1, $ symbol, to what end

url (r '^ the Test /', the Test), 
# which can be accessed during a visit in / what is behind the increase 
url (r '^ the Test / $', the Test) 
# is the need to end this / can

2, Wild

url(r'^',test)

Wild will match all the url, may be used as the page (404) is returned when the mismatched url

So we have to put the final wildcard, or he will match the first wildcard, leading to the original url inaccessible

Reverse route

The reverse route is a form for form, when our uri changes, major changes all the links have the appropriate

All have a reverse route, allowing single form of action to follow changes uri changes

Background url: setting the name attribute

url(r'^sdsdsdsad/',test,name="xxx")

Distal form form: action to subsequent changes in surface changes name

<form action="{% url "login" %}" method="post">

django create app

We put different types of logic functions and uri into a separate file, the file becomes app

There are two ways to create

  1, pycharm create a project created, you can only create a

  2, the command line is created, enter the path to the input of the next project

    python manage.py startapp custom name

We've created the app file the following documents and on

  admin.py: write and django-admin-related configuration

  apps: app's configuration

  models: Model Data Sheet

  views: view function

  tests: test

Routing packets

Copy the code
# Must first introduction method include 
from django.conf.urls Import include 
# Total the urls.py 
    URL (R & lt 'app01 ^ /', include ( 'app01.urls')) 

# minutes the urls.py App 
    # Import view function 
    from app01 import views 
    URL ( '^ Test', views.test)
Copy the code

Function view

The main function of view to write some logic function, the function is divided into two main views

1, FBV function based view write function processing logic

2, CBV class based view by handling logic

In the CBV

url need to make changes

url(r'^login/', views.Login.as_view()),

Processing logic class also needs to inherit this class View

Copy the code
View django.views Import from 
class the Login (View): 
    def dispatch (Self, Request, * args, ** kwargs): 
        Super (the Login, Self) .dispatch (Request, * args, ** kwargs) 
        # custom logic 

    def get (Self): 
        Pass 

    DEF post (Self): 
        Pass 

# If get submitted to enter the get function, post submitted to enter post function 
# bottom taking the dispatch function, he will be distributed to different functions in accordance with your submission 
# we can also override this method, add some other restrictions, such as limiting ip etc.
Copy the code

Submission

Copy the code
"" " 
Several common submission 
get: request data 
post: submit data 
delete: delete data 
put: update data 
patch: Update part of the data 
." ""

Guess you like

Origin www.cnblogs.com/huikejie/p/11204844.html