As the most popular web framework of python, django has been loved by the majority of Python. This site is based on django. Therefore, it is necessary to summarize some basic knowledge of django to lead beginners into the python web world.
1. Basic knowledge
1.1.django command parsing
python manager.py "commands" is the encapsulation of Django admin command, and the format of python manager.py commands is more used in the project
[auth] changepassword: modify admin Password createsuperuser: Create super administrator
[django] check: Check project status, including: database model status startapp: Create a new app runserver: Operation service makemigrations: tell django,Some changes have been made to the model and stored as migration files. In fact, some python Database management script for migrate: Running migration files and automatically managing databases is actually running database management scripts, creating databases, tables, etc dumpdata: Export data to json file loaddata: from json File import data shell: start-up django Of shell,The related environment variables are set, which is very convenient to use. In fact, they are python Of shell
1.2 installation environment
First, you need to have a Python dev environment
Install pip, easy install tool
pip install django - install the latest version of Django, or download the Django source code for installation
If you need to run multiple versions of Djanjo, you can install the virtual environment: pip install virtualenv virtualenvwrapper
1.3 creating projects and Applications
django-admin startproject project_name cd project_name python manager.py startapp app_name
Add the app name to settings.py in the project configuration directory
1.4 start up project
python manager.py runserver ip_addr:port
1.5 browser access
http://ip_addr:port/
This is the access mode in the development mode. In the production environment, it needs to be deployed to apache or nginx.
1.6. Project directory details
bogon:zqxt_tmpl david$ tree . ├── db.sqlite3 #Database files ├── learn #app name │ ├── __init__.py #Module initialization file │ ├── __init__.pyc │ ├── admin.py #Registered module │ ├── admin.pyc │ ├── apps.py #app configuration │ ├── migrations #Database migration │ │ ├── __init__.py │ │ └── __init__.pyc │ ├── models.py #Database module, used for reading and writing database │ ├── models.pyc │ ├── templates #html template directory │ │ └── home.html #html file │ ├── tests.py #Test code │ ├── views.py #Business logic processing, processing http requests │ └── views.pyc ├── manage.py #Project management, including multiple commands, can create app, start project, migrate database, etc └── zqxt_tmpl #Project directory ├── __init__.py #Project documents ├── __init__.pyc ├── settings.py #Project settings ├── settings.pyc ├── urls.py #Project URL processing, URL entry ├── urls.pyc ├── wsgi.py #After the project is published, the web server uses └── wsgi.pyc //In addition, there is form.py to complete the form processing
2. Deploy django project to apache
2.1 copy django project to / var/www / (or other directory)
Take the yue project as an example:
cp -r yue /var/www/ Collect static files and execute them at / var/www/yue / python manage.py collectstatic
2.2 modify Apache2 configuration file
1)ports.conf Add to listen port root@abellee:/etc/apache2# cat ports.conf # If you just change the port or add more ports here, you will likely also # have to change the VirtualHost statement in # /etc/apache2/sites-enabled/000-default.conf Listen 80 Listen 8888 <IfModule ssl_module> Listen 443 </IfModule> <IfModule mod_gnutls.c> Listen 443 </IfModule>
2) create the file yue.conf in the directory / etc / apache2 / sites available
root@abellee:/etc/apache2/sites-available# cat yue.conf <VirtualHost *:8888> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined WSGIScriptAlias / /var/www/yue/yue/wsgi.py Alias /static /var/www/yue/collected_static </VirtualHost>
3) create a soft link in the / etc / apache2 / sites enabled directory and restart the service
yue.conf -> ../sites-available/yue.conf Restart the Apache 2 service Browser execution: http://127.0.0.1:8888/
3.FAQ
Note: problems will be encountered during deployment
1.First, it must be ensured that: python /var/www/yue/yue/wsgi.py No mistake 2.If found django 403 Error, please yue/setting.py Delete in csrf Protection mechanism, directly in setting.py Mid search setting 3.Need to add django Project directory to Python Of path in wsgi.py Add to file: import sys paths =["/var/www/yue"] for path in paths: if path not in sys.path: sys.path.append(path)