The first project notes:
Create a project:
- Through the command line: first to enter the virtual environment to the installation of django. Then execute the command:
django-admin startproject [项目的名称]
This will create a project in the current directory. - By pycharm way: File -> New Project -> Select django. Then specify the path where the project, as well as the Python interpreter, and then click Create to create a project.
Run the project:
- Terminal: Go to the project folder, and then execute the following command to run:
python manage.py runserver
- pycharm: click on the arrow button in the upper right corner of the green triangle on it. Note: The pycharm run the project, to avoid a project run multiple times. . In the project configuration, the "single instance only" option checked on that, to avoid the above problems.
Change the port number:
- In the terminal: run time plus a port number on it. Command:
python manage.py runserver 9000
. - In pycharm in: the upper right corner -> Project Configuration -> port. Change the port number you want, re-run.
Let other computers in the same local area network access to the project of your machine:
- So that the amount of time to run the project, host to 0.0.0.0.
- In the end, use the
python manage.py runserver 0.0.0.0:8000
command: . - In pycharm, the upper right corner -> Project Configuration -> host. Changed
0.0.0.0
.
- In the end, use the
- In the
settings.py
file configurationALLOWED_HOSTS
, ip address added into the machine. Sample code is as follows:
python ALLOWED_HOSTS = ['192.168.0.103']
Note: To turn off your computer's firewall job.
Project structure analysis:
manange.py
: After the project and interaction are essentially based on this document. It is generally input python manage.py [subcommand] terminal. You can enter python manage.py help look can do anything. Unless you know what you're doing, you should not edit this file under normal circumstances.settings.py
: Save the project all the configuration information.urls.py
: Used to do url mapping and view functions. Came after a request, you will find the matching view function from this file.wsig.py
: Designed to make deployment. no need for correction.
Recommended django project specifications:
Stratified by function or modules into one app. And a module for all relevant views are written in the corresponding app views.py, and other models and is also similar. Then django has provided a convenient app to create command is called python manage.py startapp [app的名称]
. All the code written in the respective app.
DEBUG mode:
- If you turn on the DEBUG mode, after we modified the code Django project, and then press ctrl + s, then Django will automatically restart the project to us, do not need to manually restart.
- If you turn on the DEBUG mode, after Django project code appeared bug, then in the browser and the console will print an error message.
- In a production environment, prohibit open DEBUG mode, otherwise there is a big security risk.
- If DEBUG is set to False, you must set ALLOWED_HOSTS.
ALLOWED_HOSTS:
This variable is others can only be accessed through this variable ip address or domain name used later to set up.