Django is a very good web development framework, and here is a basic use. The specific configuration is python3.6 version, Django is version 1.11.6. Pycharm is the 2017.3 community version. What we want to achieve here is to deploy the webpage in Django and then implement the visit.
1. Deploy python3.6 virtual environment (to avoid version conflicts)
python3 -m venv / opt / mydj #Deploy the python virtual environment in / opt / mydj
Second, activate the python virtual environment
source /opt/mydj/bin/activate
3. Download Django module and pymysql module
pip install pymysql
pip install django==1.11.6
Fourth, pycharm creates a new project named myweb, and sets the interpreter to a python virtual environment
5. Switch to the pycharm project and create a new Django project called mysite
cd PycharmProjects/myweb/
django-admin startproject mysite
Six, switch to the Django project directory, start Django
cd mysite/
python manage.py runserver 0:80 #Use the python virtual environment interpreter to start the manage.py script, start the service, and occupy port 80
Seven, Django uses the text database such as db.sqlite3 by default, and our actual situation is to use a relational database, etc., so here we modify the database used by Django. Create such a database in mysql, create database mydj default charset utf8; and then modify the configuration of Django.
mysql> create database mydj default charset utf8;
Check pycharm to see what are the files
Modify the configuration file settings.py
ALLOWED_HOSTS = '*' #Allow all hosts to access
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases Data can modify the official link of the template
DATABASES = { #Replace Django's database
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydj',
'USER': 'root',
'PASSWORD': '123456' ,
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
LANGUAGE_CODE = 'zh-hans' #language
TIME_ZONE = 'Asia/Shanghai' #时区
USE_TZ = False #Close
After modifying the configuration, Django will automatically restart the program, here is an error. It can be seen from the error report that the module supporting mysql is missing.
Modify the __init__.py file
The program runs normally.
8. Delete the text database file of db.splite3.
rm -rf db.splite3
Nine, create a new Django application. The understanding of the application here is that an application is a set of specific websites.
python manage.py startapp polls
Look at the project file in pycharm again
Ten, Django's workflow is like this. The client accesses the website, matches by the URLS route, sees which view function is sent to be processed, and then the view function connects to the model (database) for data processing, and then sends the processing result to the template (HTML) to feed back to the customer end. Follow this line of thought. We should first modify the URLS route, then modify the view function, then modify the model, and then modify the template. But this article only deploys Django environment. So let ’s simplify.
1. Modify the project urls.py file to authorize the application of urls routing for routing functions
Interpretation of this new article added here: When the access matching the beginning of polls is handed over to the urls below the polls for processing.
2. Added urls.py routing file under polls
Interpretation of the newly added code here: when the match is empty (starting with polls, empty, it means polls or polls /), start the index view function, the function name is index
3. New view function index, view.py
The representative here is to add a new view function called index, import the HttpResponse module, and directly feedback the string to the client.
Django is running normally. To this Django environment, deployment is complete.