The first django project notes

The first project notes:

Create a project:

  1. 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.
  2. 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:

  1. Terminal: Go to the project folder, and then execute the following command to run:
    python manage.py runserver
  2. 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:

  1. In the terminal: run time plus a port number on it. Command:python manage.py runserver 9000 .
  2. 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:

  1. 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:8000command: .
    • In pycharm, the upper right corner -> Project Configuration -> host. Changed 0.0.0.0.
  2. In the settings.pyfile configuration ALLOWED_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:

  1. 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.
  2. settings.py: Save the project all the configuration information.
  3. urls.py: Used to do url mapping and view functions. Came after a request, you will find the matching view function from this file.
  4. 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:

  1. 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.
  2. 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.
  3. In a production environment, prohibit open DEBUG mode, otherwise there is a big security risk.
  4. 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.

Guess you like

Origin www.cnblogs.com/jin-ting/p/11758072.html