1. Create Project
cd to the directory you want to create the project. Run the following command
django-admin startproject HelloWorld
Creating the HelloWorld project
View project HelloWorld, the directory structure is as follows:
Container project: the HelloWorld
manage.py: command-line tool for Django and interactive
HelloWorld / the init .py: empty file that tells python, the directory is a Python package
HelloWorld / setting.py: configuration file for the project
HelloWorld / urls.py: url declare the project
HelloWorld / wsgi.py: server portal
2. Create an application
Under the project HelloWorld, learn to create applications
django-admin startapp learn
or
python manage.py startapp learn
3 running on a development server project (Development Only)
python manage.py runserver
Enter the local browserhttp://127.0.0.1:8000,查看HelloWorld 工程的运行页面。
python manage.py runserver 0.0.0.0:8000
0.0.0.0 means to monitor all the ip address of the computer, and 8000 is the port that can be self-made.
4 uwsgi run the project
uwsgi
-
-
http :
8001
-
-
chdir
/
path
/
to
/
project
-
-
home
=
/
path
/
to
/
env
-
-
module project.wsgi
--home virtualenv specified path, can be removed if not. project.wsgi refers to a project / wsgi.py file
5 using supervisor
1, generates supervisor default configuration file, for example, we put /etc/supervisord.conf path, root user to perform
echo_supervisord_conf >
/
etc
/
supervisord.conf
2, supervisor.conf added at the bottom
[program:zqxt]
command=/path/to/uwsgi --http :8003 --chdir /path/to/zqxt --module zqxt.wsgi
directory=/path/to/zqxt
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true
(
sudo
) supervisord -c
/etc/supervisord
.conf
(
sudo
) supervisorctl -c
/etc/supervisord
.conf [start|stop|restart] [program-name|all]
[uwsgi]
socket =
/home/tu/zqxt/zqxt
.sock
chdir =
/home/tu/zqxt
wsgi-
file
= zqxt
/wsgi
.py
touch
-reload =
/home/tu/zqxt/reload
processes = 2
threads = 4
chmod
-socket = 664
chown
-socket = tu:www-data
vacuum =
true
(
sudo
) supervisorctl -c
/etc/supervisord
.conf restart zqxt
FAQ:
1, browser access error
Invalid HTTP_HOST header: '192.168.1.111:8000'. You may need to add '192.168.1.111' to ALLOWED_HOSTS
Solution:
Changes in project setting.py in ALLOWED_HOSTS = [ '*']
2, how to add applications to the project
Our new application (learn) added to the project's settings.py in INSTALLED_APPS, that is told there is such a Django application
INSTALLED_APPS
=
(
'django.contrib.admin'
,
'django.contrib.auth'
,
'django.contrib.contenttypes'
,
'django.contrib.sessions'
,
'django.contrib.messages'
,
'django.contrib.staticfiles'
,
'learn'
,
)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
mysql database configuration
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'hellodatabase',
'USER': 'zhj',
'PASSWORD': '12345678',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
NOTE: django using default mysql MySQLdb module connector. But not only this module python3 python2 have this module, python3 only pymysql module, so we need to take the initiative to amend pymsql:
import
pymysql
pymysql.install_as_MySQLdb()
Open the terminal input both commands:
. 1, Python manage.py makemigrations
2,python manage.py migrate
manage.py makemigrations find all of our models through the python, and then migrate to generate a database table by python manage.py,
4 service supervisor start start error
Starting supervisor: Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting superviso
Solution: unlink / tmp / Supervisor . Our sock
Reason: The process can already started, you need to unlink restart before.
5, do not know the program or Web page text django running
Press said the Internet try the following:
1), in the first line of the file .py plus # coding = UTF-8
2) adding the .py file
import sys
var1=sys.getdefaultencoding()
reload(sys)
sys.setdefaultencoding('utf8')
We have failed to resolve the issue.
Finally, the program file itself is found wrong format, are ASCAII text types.
Check the file format command:
file file_name or
enca -L zh_CN file_name
As long as this type of file format conversion utf-8 can solve this problem.
Modified in pycarm editor, such as adding a line in the file = s 'from the' Save Chinese, can convert over, of course, remove the Chinese therein, will return ASCAII text type.
6, how to shut down apache service
sudo apache2ctl stop
sudo update-rc.d -f apache2 remove -f parameter is mandatory to delete symbolic link, even if /etc/init.d/apache2 still exists. Note: This command only disable the service until the service is upgraded.
If you want to remain disabled after the service upgrade. Should execute the following commands: update-rc.d apache2 stop 80 0 1 2 3 4 5 6.
7, django does not create problems in the database table
报错: No migrations to apply
first step:
__Init__.py file migrations, etc. in case of deletion of the app name.
Step two:
Access to the database, find django_migrations table, delete all records of the app name.
The third step: execute this command :( two in the project directory)
python manage.py makemigrations
python manage.py migrate
the reason:
django_migrations modifying records in the database table records the correspondence table.
After each modification, the third step is executed command, and then modify the file folder generated in the first step of the file, django_migrations modification of table records the change process.
References:
https://code.ziqiangxuetang.com/django/django-url-name.html
http://www.runoob.com/django/django-tutorial.html
http://www.runoob.com/python/python-tutorial.html