In the process of using Django, you need to open the Python and cmd command windows at the same time, as well as the web editor
.py type file, which needs to be opened with Python.
Command window instructions are uniformly preceded by the $ mark. When copying the code, do not copy the $
.html type files, with a % mark in front, can be edited with notepad, or with webstorm, both of which are relatively easy.
1. Install Django (windows system)
Run cmd to enter the command window and enter:
$ pip3 install Django==1.11.11
After waiting for the installation to complete, enter:
Django-admin #Show the following picture to indicate that the installation is successful
2. Create a working folder
My working folder is set at "E:\Django\1", and I cd to that location.
Notice:
manage.py is a small package of django-admin. It is very convenient to call manage.py to execute commands such as creating a new APP and starting a server. We will often use it in future debugging. To cd to the folder where the manage.py file is located, you can run
3. Necessary settings
Before officially writing our website, we also need to do some preparations for the website structure.
Create two new folders: static and templates
1. static
It is used to store css classes, js classes and other files introduced by html<head>, as well as static files such as pictures and sounds.
Most of the static files shared by web pages can be directly stored in the static directory, or you can set up folders such as image and sound to manage files of a certain type.
The purpose of doing so is obvious and easy to manage! ! For a large website project, if you don't want to search for a needle in a haystack of hundreds of files, keep the files sorted from the very beginning.
2. templates
Used to store html class files, and also create a new folder for each app.
After doing the above, our website is just an empty shelf. Next, you need to tell Django about the connection between the various components.
Supplementary templates association settings
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', ], }, }, ]
Added code snippet explanation to tell Django the location of the templates folder.
Modify the system time to Beijing (Shanghai) time
TIME_ZONE = 'Asia/Shanghai'
Supplementary static association settings
#Alias of static file save directory STATIC_URL = ' /static/ ' #All static files (css, js, images) are configured in this folder STATICFILES_DIRS= [ os.path.join(BASE_DIR,'static'), ]
Added a code snippet to explain, telling Django that it involves the {% load static %} command, go to the static folder to find it.
After assigning the path of each folder, we need to uniformly schedule the address (url) of the website.
#Open urls.py from django.conf.urls import url from django.contrib import admin from django.shortcuts import HttpResponse,render def xixi(request): return render(request, '../templates/xixi.html') def haha(request): return render(request, '../templates/haha.html') def login(request): return render(request,'../templates/login.html') urlpatterns = { url(r ' ^xixi/ ' , xixi), #Add correspondence url (r ' ^haha/ ' , haha), url(r'login',login) }
The newly added code above contains a regular expression r'^xixi', where the symbol ^ indicates a header match.
The overall directory of this test project file is as follows:
Fourth, run the server
Ok, now let's use this machine as a server and take a look at our website
Call manage.py, run the code
python manage.py runserver
Our server address is at " http://127.0.0.1:8000/ "
Enter the address " http://127.0.0.1:8000/xixi " in the browser address bar
Enter the address " http://127.0.0.1:8000/ haha " in the browser address bar
Enter the address " http://127.0.0.1:8000/login " in the browser address bar
Summarize the role of each Django module:
urls are responsible for assigning web page addresses to views
views are responsible for passing variables to templates
templates receive variable values and display them to visitors