Directory
First, install Django
Ii. Creation of the project
Third, create the app
Iv. Static files
Five, template path
Vi. Setting settings
Vii. Defining routes
Viii. Defining views
Nine, rendering templates
Ten, run
Django is a Python-style web framework
First, install Django
PIP3 Install Django
After the installation is complete C:\Python35\Script will generate Django-admin
Ii. Creation of the project
Django-admin Startproject Project Name
such as Django-admin Startproject MySite
MySite
-MySite
__init__.py
settings.py # configuration file
urls.py # URL Correspondence relationship
wsgi.py # Follow the WSGI specification, the actual production environment Uwsgi+nginx
manage.py # Managing Django Programs
Third, create the app
Python manage.py Startapp CMDB
Python manage.py startapp Xxoo ...
App Catalog
-Migrations Data modification table structure record
admin.py Django provides us with back-office management
apps.py Configuring the current app
models.py ORM, which writes the specified class, can create a data structure by command
tests.py Unit Test
views.py Business Logic Code
Iv. Static files
Configuring a static file directory
Staticfiles_dirs = (Os.path.join (base_dir, "Static"),)
Five, template path
path to the configuration template
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 ', ], }, },]
Vi. Setting settings
Comment out
' Django.middleware.csrf.CsrfViewMiddleware '
Vii. Defining routes
From django.conf.urls import urlfrom django.contrib import adminfrom CMDB import viewsurlpatterns = [url (r ' ^admin/', a dmin.site.urls), # URL (r ' ^index/', views.index), url (r ' ^login ', views.login), url (r ' ^home ', views.home),]
Viii. definition View
from django.shortcuts import render# create your views here.from django.shortcuts import httpresponsefrom django.shortcuts import renderfrom Django.shortcuts import redirectdef index (Request): return HttpResponse ("
def func (Request): # Request.method Get/post # request. Get.get ("", None) gets the requested data # request. Post.get ("", None) gets the data submitted by the request return HttpResponse ("string") return render (Request, "HTML template path") return redirect ("/U RL path ")
Nine, rendering templates
<! Doctype html>
Template rendering
Variable {{variable name}}
def func (Request): Return render (Request, "HTML template", {"Current_User": "Alex"})
<!--template-->
Condition Judgment {% if condition%}{% else%}{% endif%}
def func (Request): Return render (Request, "HTML template path", {"Current_User": "Alex", "User_list": ["Alex", "Eric"], "Age": 18})
{% if age%} <a> ages </a> {% if aging >%} <a> old man </a> {% Else%} <a> small Meat & lt;/a> {% endif%}{% else%} <a> no age </a>{% endif%}
For loop {% for row in user_list%}{% endfor%}
def func (Request): Return render (Request, "HTML template path", {"Current_User": "Alex", "User_list": ["Alex", "Eric"]} )
Ten, run
Python manage.py runserver 127.0.0.1:8000
This article is from the "Eight Miles" blog, so be sure to keep this source http://5921271.blog.51cto.com/5911271/1926729
Python Road 58-django installation configuration and some basic points of knowledge