DjangoImplementation process
Django Learning Framework:
#安装: PIP3 Install Django
Adding environment variables
#1 Create Project
Django-admin Startproject MySite
---mysite
---settings.py
---url.py
---wsgi.py
----manage.py (startup file)
#2 Create an App
Python mannage.py Startapp App01
#3 settings Configuration "Static files (CSS, JavaScript, Images)"
TEMPLATES
Staticfiles_dirs= (
Os.path.join (Base_dir, "statics"),
)
Static_url = '/static/'
# we can only use Static_url, but Static_url will follow your staticfiles_dirs to find
#4 design code based on requirements
url.py
view.py
#5 using templates
Render (req, "index.html")
#6 Startup Project
Python manage.py runserver 127.0.0.1:8080
#7 Connect to the database, manipulate the data
model.py
Django Installation and project creation
Installation: PIP3 Install Django
Create a Django project MySite and subproject Blog
Introduction to project Documents
Preliminary sledgehammer
Case one: Basic use
settigs.py: Change configuration of Django2.0.1, update to previous path configuration
' DIRS ': [Os.path.join (Base_dir, ' templates ')], # Django Previous version
views.py
From django.shortcuts import Render, the httpresponse# request encapsulates all the requested information # HttpResponse: As with request, the returned information can be rendered to the page def Hello (Request): return HttpResponse ('
mysite2/urls.py
From Django.contrib import adminfrom django.urls import pathfrom Blog Import viewsurlpatterns = [ path (' admin/', admin . site.urls), Path (R ' hello/', Views.hello) # Map path names to functions)
]
To start the service side:
Python manage.py runserver 8080
Another way to start: Pycharm provided, the default 8000 port, can be modified, this article with 8080
Note : [If there is a modification, the modification can be run directly in the browser, Python has been processed within]
Interface rendering:
Case two: Invoking HTML to implement page rendering
settigs.py: Change configuration of Django2.0.1, update to previous path configuration
' DIRS ': [Os.path.join (Base_dir, ' templates ')], # Set templates path to Django previous version # ' DIRS ': [], # comment out the line, this is the Django 2.0.1 Latest Version # ' Django.middleware.csrf.CsrfViewMiddleware ', # comment out the line
mysite2/urls.py
From Django.contrib import adminfrom django.urls import pathfrom Blog Import viewsurlpatterns = [ path (' admin/', admin . site.urls), Path (R ' hello/', Views.hello) # Map path names to functions
Templates/today.html
<! DOCTYPE html>
views.py
The From django.shortcuts import Renderimport datetime# request encapsulates all the information requested by Def hello (request): # The bottom of render is called HttpResponse for rendering, and Django returns to the page is the HttpResponse object # render (): Used to render the browser display, with the HTML implementation of the exchange of data # Here the first parameter must be rquest, the second parameter is the name of our page HTML, and the third parameter is the passed parameter # here the HTML name is directly written because Django will encapsulate the parameters in settings.py file # ' DIRS ': [ Os.path.join (Base_dir, ' templates ')], # Django Previous version today = Datetime.datetime.now () return render ( Request, "today.html", {"Today": Today})
Page rendering
Case three: Receive foreground data and return to foreground data
settigs.py: Change configuration of Django2.0.1, update to previous path configuration
' DIRS ': [Os.path.join (Base_dir, ' templates ')], # Set templates path to Django previous version # ' DIRS ': [], # comment out the line, this is the Django 2.0.1 Latest Version # ' Django.middleware.csrf.CsrfViewMiddleware ', # comment out the line
views.py
From django.shortcuts import Render, Httpresponseimport datetimedef userInfo (Request): user_list = [] # Here you need to determine whether the foreground data submission method is post or get [uppercase] if Request.method = = "POST": username = Request. Post.get ("username", None) sex = Request. Post.get ("Sex", None) email = Request. Post.get ("email", None) user = {"username": username, "sex": Sex, "email": Email} Print (user) User_ List.append (user) return render (Request, ' index.html ', {"User_list": User_list}) # Passing an object to the front end with {}
mysite2/urls.py
From Django.contrib import adminfrom django.urls import pathfrom Blog Import viewsurlpatterns = [ path (' admin/', admin . site.urls), Path (R ' userinfo/', Views.userinfo), # Map path name to function)
Templates/index.html
<! DOCTYPE html>Page rendering
another: If the foreground authentication error, you need to change a Django security configuration
settings.py
Python Learning---Django basic learning