Ape to learn python django tutorial Tutorial 1 first met Django

# Install django

` pip install django==2.2.*`

# Create Project

` django-admin startproject web `

# Start the project into the project directory, in the same directory manage.py file, execute the command

` python manage.py runserver `

```python

......

October 14, 2019 - 09:04:19

Django version 2.2.6, using settings 'web.settings'

Starting development server at http://127.0.0.1:8000/

Quit the server with CTRL-BREAK.

```

# Open a browser to access http://127.0.0.1:8000/

```

tree ./web /F

The basic directory structure

/WEB

│  manage.py│

└─web

    │  settings.py

    │  urls.py

    │  wsgi.py

    │  __init__.py

```

# Create Application

` python manage.py startapp myhome `

## outputs a hello world

### 1. Create a good application, write view view function myhome / views.py

```python

from django.shortcuts import render

from django.http import HttpResponse

# Create your views here.

def index(request):

 

    return HttpResponse('Hello World!!!')

```

2. ### function to view the current configuration of a route myhome / urls.py

```python

from django.urls import path

from . import views

urlpatterns = [

    path('', views.index),

]

```

Configuration application ### in the current routing path to the root web / urls.py

```python

from django.contrib import admin

from django.urls import path,include

urlpatterns = [

    path('admin/', admin.site.urls),

    path('', include('myhome.urls')),

]

```

## use the template in the project

## to modify the configuration directory settings.py/TEMPLATES/DIRS settings.py template engine

` 'DIRS': [os.path.join(BASE_DIR,'templates')], `

### 1. Create templates folder in the same directory manage.py file

### 2. Create a template in the template file .html file folder

### 3. Use the template file myhome / views.py in view function

```python

def func(request):

    return render(request,'a/ind.html')

```

## Use static files (css, js.img ...) in the project

### to modify the configuration directory settings.py settings.py template engine

```python

STATIC_URL = '/static/'

STATICFILES_DIRS  = [

    os.path.join(BASE_DIR,'static')

]

```

### 1. Create a static file folders in the same directory manage.py

### 2. Create a static file in the static folder

### 2. Using a static template files in the folder /static/js/1.js

The current project directory structure

```

\ WEB - project directory

│ db.sqlite3 - django default database configuration, database files generated

│ manage.py - manage files, the current project only entrance file

├─myhome - custom-created application

│  │  admin.py     

│  │  apps.py     

│ │ models.py - model files in the current application

│  │  tests.py

│ │ urls.py - routing files in the current application (sub routing file)

│ │ views.py - in view of the current application function

│  │  __init__.py

│  ├─migrations

│  │      __init__.py

├─static - static folder

│      1.css

├─templates - Template folder

│  └─a

│          ind.html

└─web - directory and same name as the project, the current configuration and management of the project ...

    │  settings.py

    │  urls.py

    │  wsgi.py

    │  __init__.py

```

Some related concepts

routing:

    Url is defined when the route to user access, and the url defined path and generates the map corresponding to the view function

view:

    Is a function or method, can also be defined as a class,

    Mainly for receiving a user's request, and responds

    Logic code in the main program in view of the function

template:

    A template engine django frame, can be done and the python logic code separation html

    In view of the function and the need to give the user response template, return or transfer data

Static file:

    Require the use of specialized storage directory in the template of static files, css, js, font, img, vido

model:

    Model is designed for processing data layer

    In django frame, by defining a model class, to achieve the management data in the database (CRUD)

    In development, the operation of the data class carried, are mapped to specific implementation (SQL) database, the conversion paired data

Design framework (design patterns)

main idea:

    It is to show the logic code, control data, and the page completely separated

    Reduce the coupling between a program module (Low coupling, high cohesion)

MVC design pattern

M Model Model ==> layer management data

V View View Management page ==> Display Modules

C Controller Controller ==> code management logic

MVT design patterns Django, Flask

M Model ==> Model ==> layer management data, change data search deletions

V View ==> View ==> layer management logic, the logic code, control flow ...

T Template ==> Templates ==> Administrative Templates, the page display, html ..

A simple request procedure

1. corresponding to a user input in the browser url, request initiated

2. We project wsgi able to accept the corresponding request,

3 and to the routing request to the root, web / urls.py

4. root file route, developed in accordance with the current user request url the request corresponding to the distributed application

5. Application of the sub-routes will continue to resolve the request url view corresponding function

6. The view function, the user receiving the request, will make corresponding response (string, the template file)

 

 


 

Mastery learning method, as will bend to overtake!

Learning to ape: the achievements of their own just a boutique!

Guess you like

Origin www.cnblogs.com/itxdl/p/12551257.html