Django framework - Basic

A small problem:

What is the root directory: that there is no path, only the domain name. url (r '^ $')

Supplementary wsgiref module on a map

 

 MTV model

Django's MTV represent:

  Model (model): database and related business objects and object is responsible for the database (ORM)

  Template (template): put all the html file
          template syntax: The aim is the independent variable (the content of the database) how cleverly embedded in html page

  View (View): is responsible for the business logic, and calls the Model and Template at the appropriate time

  In addition, Django there is a URL dispatcher. His role is to be a one page URL requests are sent to different treatment of Views, Views and then call response Model

And Template.

 

 

 

 

 

 Django basic commands

1. Download Django:

pip3 install django

 

 2. Create a Django objects

django- admin.py startproject project name 
Django -admin.py startproject mysite

 

 After you create the success of such a project will generate. Directory structure is as follows:

 

 

  •  manage.py -------- startup file (Django project inside the tool by which you can call the number Django shell and databases)

  • settings.py ----------- contains some set of projects, including database information, mode flags, and other variables to work.

  • urls.py -------------- mapping between the path and the view function

3. Create an application

python3 manage, py startapp blog (application name)

 

 After you create the success of such a project will generate. Directory structure is as follows:

 

 

 4. Start Django project

python3 manage.py runserver  8080

 

 So that our django started up when we visit:! Http: //127.0.0.1: 8080 / when you can see:

 

 

 5. Create Table command

python3 manage.py makemigrations

python3 manage.py migrate

 

 

The routing configuration view layer system (views)

URL configuration (URLconf) as the Django directory site support. His nature is a mapping table between the URL and the URL you want to call that view function;

You're in such a way to tell Django, call the code for this URL, call that code for that URL.

'' ' 
    
    The urlpatterns = [ 
         URL (regex, views view function, parameter, alias) 
] 


Parameters: 

    a regular expression string 
    a callable, usually a string path view function is designated as a view or function 
    the optional parameters to be passed to the default view function (a dictionary) 
    an optional parameter name 

    '' '

Parameters of the string that URLconf

1. Simple Configuration

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^articles/2003/$', views.special_case_2003),
    url(r'^articles/([0-9]{4})/$', views.year_archive),
    url(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),
    url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail),
]

Note that:

'' ' 
    NOTE: 
. 1, once the match is successful discontinued 
2, to capture a value from the URL, only to place a pair of parentheses around it. 
3, do not need to add a leading backslash, because each URL has. For example, it should be ^ articles, not ^ / articles. 
4, the front of each regex 'r' is optional but suggested adding. 

Some examples request: 

    / Articles / 2005/3 / URL does not match any pattern, because the list of the third month should be the model requires two numbers. 
    / articles / 2003 / The first match in the list mode is not the second, because the pattern matched in sequence, the first one will be the first test match. 
    / articles / 2005/03 / request list matches the third mode. Django would call the function 
                       views.month_archive (request, '2005', '03'). 
    '' '
# Is set to open URL to access the item is not following the address / jump to with the / path 
APPEND_SLASH = True

 

Guess you like

Origin www.cnblogs.com/s686zhou/p/11528645.html