Django installation and initialization

Getting to know Django

Django was originally designed for news sites with rapid development needs, with the goal of realizing simple and fast website development.

Install Django

Install the official release version via pip

$ python -m pip install Django

View version number

$ python -m django --version

Create project

If this is your first time using Django, you need some initial settings. In other words, you need to configure a Django project with some automatically generated code - that is, a collection of settings required by a Django project instance, including database configuration, Django configuration, and application configuration.

Open the command line, cd to a directory where you want to put your code, and then run the following command:

$ django-admin startproject [projectName]

Directory after creation

projectName/
    manage.py
    projectName/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py

• The outermost projectName/ root directory is just the container of your project. The root directory name has no effect on Django. You can rename it to any name you like.
• manage.py: A command line tool that allows you to manage Django projects in various ways.
• The projectName/ directory at the inner level contains your project, which is a pure Python package. Its name is the Python package name you need to use when you refer to anything inside it. (Such as projectName.urls).
• projectName / the init .py: an empty file that tells Python that this directory should be considered a Python package.
• projectName/settings.py: The configuration file of the Django project.
• projectName/urls.py: The URL declaration of the Django project, just like the "directory" of your website.
• projectName/asgi.py: As the entry point of your project running on an ASGI compatible web server.
• projectName/wsgi.py: As the entry point of your project running on a WSGI compatible web server.

Start the django server

Let's confirm whether your Django project is really created successfully. If your current directory is not the outer mysite directory, please switch to this directory, and then run the following command:
/ 

$ python manage.py runserver

After startup, you can see the prompt, the default service address is 127.0.0.1: 8000

Performing system checks...

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.

八月 03, 2020 - 15:50:53
Django version 3.0, using settings 'projectName.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Replace port

By default, the runserver command will set the server to listen on port 8000 of the internal IP of the machine.

If you want to change the listening port of the server, please use the command line parameters. For example, the following command will make the server listen on port 8080:
/ 

$ py manage.py runserver 8080

If you want to change the IP that the server listens to, enter the new one before the port. For example, in order to monitor the public IP of all servers (this is useful when you are running Vagrant or want to show your results to other computers on the network), use:

$  py manage.py runserver 0:8000

0 is short for 0.0.0.0.

Guess you like

Origin blog.csdn.net/p715306030/article/details/113139326