day49 django

http protocol

Four characteristics

1. Based on protocol TCP / IP application layer acting above
2. Based on the request response
3. stateless
4. No connection

Data Format

Request format
request the first row
request header
/ r / n
Request Body

The response status code

A string of numerical language interpreter

Django

Pure hand line and simple web framework

wsgiref help you roll up on third-party modules

views.py put inside view function is a function called the View layer

urls.py put inside routing (suffix) function of the correspondence between the view routing layer

templates folder inside put the HTML code template layer

Static and dynamic pages

Static pages: the data is written to die the same years, even if the change is man-made modified directly

Dynamic pages: data is acquired in real time

eg:. 1 back-end code dynamically obtain the current time
2. The data from the database query out

Topic 1: access the page, the page shows the current access time
Topic 2: access the page, showing the back-end user dictionary on the front page and page through some simple way to promote the value of dictionary

jinja2 template syntax

Designed to interact with back-end data processing and HTML pages

Template syntax (grammar extremely close to the python backend)

Lets you in on an HTML page, it is possible to use back-end operations python syntax to pass over the back-end data

        <p>{{ userDic }}</p>
        <p>{{ userDic.username }}</p>
        <p>{{ userDic['age'] }}</p>
        <p>{{ userDic.get('hobby') }}</p>
        <p>{{ userDic.get('hobby').0 }}</p>
        <p>{{ userDic.get('hobby').1 }}</p>
        <p>{{ userDic.get('hobby').2 }}</p>
        
        {% for user_dic in user_list %}
            <tr>
                <td>{{ user_dic.id }}</td>
                <td>{{ user_dic.username }}</td>
                <td>{{ user_dic.password }}</td>
            </tr>
        {% endfor %}

Rendering template are passed on to the back-end data HTML file, in the back-end deal to produce a complete process HTML file (is finished at the back end and front-end does not matter)

three major python web framework

Django: large and built-in components and functions very much like an aircraft carrier. Weaknesses: write a small project, it may be more cumbersome (overkill)

flask: small but dapper own components and is particularly low, substantially all rely on third-party components. Similar to the Rangers. Shortcomings: the impact is limited by the relatively large third-party modules. If the flask all third-party modules together, directly overshadowed Django

tornado: asynchronous non-blocking even be used to develop the game server.

Custom frame:
A: Socket part
b: Match route
c: Template grammar

django: a use of someone else's wsgiref (django default), b write their own, c write their own
flask: a use someone else's werkzeug, b wrote it myself, c use of someone else's jinja2
Tornado: A, b, c are write your own

Django framework introduced

Notes:
1. The computer name can not have Chinese
2.python interpreter version 3.7 is recommended not to use 3.4 to 3.6
3. A window can only run a python project

Django version of the problem:
Django-based versions 1.11.11 (1.11.9 - 1.11.13)

Django how to verify that the installation was successful:
Command Line knock django-admin

Command line to create the project:

1. Create a project django
django-admin startproject project name (such as mysite)

2. Start Django project:
Switch to the project folder:
python3 manage.py the runserver (my computer manage.py the runserver Python)
python3 manage.py the runserver 127.0.0.1.8080 (designated ports)

3. Create application (django support multi-app development)
python3 manage.py startapp app name

Note:
1. does not automatically help you create templates folder
2. configuration file does not automatically help you write templates file path

app concept :
Django is to develop a functional web app as the main framework,
app is meant application application.

A django project is a university (empty shelves, does not have any function),
while the app is similar to the inside of each University

An empty django does not have any effect, just to make app pre-configured environment

The function can not move, a plurality of app developing
an app corresponding to a specific function module:
app user-related functions of the user related to
the order of the order-related functions associated app
merchandise related functions related merchandise app
each app has its own independent function

Creating good app requires registration to take effect in django profile

INSTALLED_APPS = [
            'django.contrib.admin',
            'django.contrib.auth',
            'django.contrib.contenttypes',
            'django.contrib.sessions',
            'django.contrib.messages',
            'django.contrib.staticfiles',
            # 'app01'  # 简便写法
            'app01.apps.App01Config'  # 最完整的写法
        ]

pycharm create project

1. Create an application, you can use simple manner
Tool >>> run manage.py task

startapp application name

2. Be sure the same port at the same time can only start a project django

3. The path for the configuration file templates folder, if it is empty require manual configuration

TEMPLATES = [
                    {
                        'BACKEND': 'django.template.backends.django.DjangoTemplates',
                        'DIRS': [os.path.join(BASE_DIR, 'templates')]
                        ,
                        'APP_DIRS': True,
                        'OPTIONS': {
                            'context_processors': [
                                'django.template.context_processors.debug',
                                'django.template.context_processors.request',
                                'django.contrib.auth.context_processors.auth',
                                'django.contrib.messages.context_processors.messages',
                            ],
                        },
                    },
                ]

django file function

django project name
file in the project folder with that name
setting.py exposed to the user profile can be configured
urls.py routing and view function correspondence relationship
manage.py django file entry
application name folder
migrations all folders database-related operations recorded
admin.py django admin background management
apps.py registered app using
models.py put all database-related model class
tests.py test file
views.py business logic processing function view

django basic configuration and Notes

django white will be three tricks

Returns a string HttpResponse

render returns html file, you can pass values ​​to the html page

def login(request):
        user_dic = {'username':'jason','password':'123'}
        return render(request,'login.html',{'xxx':user_dic})

redirect redirect

1. You can write directly to the path suffix site
2 can also be a full path

def home(request):
    # return redirect('/login')  # 重定向
    return redirect('https://www.baidu.com')  # 重定向

note:

The default is automatic restart django

Restart mechanism

There are detection mechanisms, real-time detection of changes in the index file. Sometimes the situation you will have not finished with it, they automatically are given, do not control

After each finished code to manually restart

Guess you like

Origin www.cnblogs.com/zqfzqf/p/11908906.html