Installation
Django is a Python-language environment, so make sure you have Python installed on your computer first.
Linux
Ubuntu:
sudo pip install django==1.11.7
A version is specified in the installation, you can modify the version number, or you do not specify that the version that is already in the software source will be installed.
Once the installation is complete, you can view the version in Python interactive mode:
>>> Import django>>> Print (Django.get_version ()) 1.11.7
The above is a simple and common method, in addition to this method, you can also download the Django source code to install:
git clone https://github.com/django/django.git
In the current directory, you will see a directory named Django with the latest version of Django, followed by the following actions in the current directory:
sudo pip install-e./django
The system will prompt Django for information that has been successfully installed: "Successfully installed Django".
Windows
Execute in CMD:
Pip Install django==1.11.7
Create a project
Method One:
Λdjango-admin startproject mysiteλtree/fd:.└─mysite │ manage.py │ └─mysite settings.py urls.py wsgi.py __init__.py
Method Two, there is a space after the project name MySite, and then there is a period (the English half-width period), so you can create the project, except that it is created under the current folder.
Λdjango-admin startproject mysite. λtree/fd:.│ manage.py│└─mysite settings.py urls.py wsgi.py __init__.py
Start the service
Enter the same directory as the manage.py execution:
Python manage.py runserver
You will see the following information:
Starting development Server at Http://127.0.0.1:8000/Quit, the server with Ctrl-break.
When you enter Http://127.0.0.1:8000/or http://localhost:8000/in your browser, you will see the results as shown.
Create an App
The project has been created, the site is there, and the next step is to implement the specific functionality of the site, in Django, where people call these specific functions "applications" (application).
Λpython manage.py Startapp blogλ tree/f D:. │db.sqlite3│manage.py │├─blog││admin. py││apps.py││models.py ││tests.py││views.py ││__init__.py││ │└─migrations│__init__.py│ └─mysite│settings.py │urls.py│wsgi.py│__init __.py│└─__pycache__ SETTINGS.CPYTHON-36.PYC URLS.CPYTHON-36.PYC WSGI.CPYTHON-36.PYC __INIT__.CPYTHON-36.PYC
Here is a brief description of the generated file
Django's Task Management command-line tool django-admin.py
Λdjango-admin Type ' Django-admin help <subcommand> ' for help on a spec IFIC subcommand. Available subcommands: [Django] Check Compilemessages createcachetable Dbshell Diffsettings DumpData Flush Inspectdb LoadData makemessages Makemigrations Migrate Runserver Sendtestemail Shell Showmigrations Sqlflush Sqlmigrate Sqlsequencereset Squashmigrations Startapp Startproject Test TestServer
View Help information:
D:\lcg\mysiteλdjango-admin Help Startapp Usage:django-admin Startapp [-h] [--version] [-v {0,1,2,3}] [--settings Settings] [--pythonpath Pythonpath] [--traceback] [--no-color] [--template template] [--extension EXTENSIONS] [--name FILES] name [directory] Creates a Django app directory Structure for the given app name in the current directory or optionally in the given directory. Positio NAL arguments: Name name of the application or pro Ject. Directory Optional Destination Directory Optional arguments: -H,--help show this help message and exit--version Show program ' s version number and exit-v {0,1,2,3},--verbosity {0,1,2,3} Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output --settings Settings The Python path to a settings module, e.g. "Myproject.settingS.main ". If this isn ' t provided, the DJANGO_SETTINGS_MODULE environment variable would be Used. --pythonpath Pythonpath A Directory To add to the Python path, e.g. "/home/djangoprojects/myproject". --traceback Raise on CommandError exceptions--no-color Don ' t color ize the command output. --template template The path or URL to load the template from. --extension EXTENSIONS,-e EXTENSIONS the file ex Tension (s) to render (default: "py"). Separate multiple extensions with commas, or use-e multiple times. --name files,-n files The file na Me (s) to render. Separate multiple extensions with commas, or use-n multiple times.
manage.py
D:\lcg\mysiteλpython manage.pytype ' manage.py help <subcommand> ' in a specific subcommand. Available Subcommands:[auth] changepassword createsuperuser[contenttypes] remove_stale_contenttypes [Django] Check compilemessages createcachetable dbshell diffsettings dumpdata flush Inspectdb loaddata makemessages makemigrations migrate sendtestemail Shell Showmigrations sqlflush sqlmigrate sqlsequencereset squashmigrations Startapp Startproject test testserver[sessions] clearsessions[staticfiles] collectstatic Findstatic Runserver
In contrast to Django-admin, the code is found to be part of the same, while manage.py has its own characteristics.
The Django-admin command corresponds to the django-admin.py file, which is stored in the Django installation directory after the Django installation. (C:\python36\scripts,linux of Windows/bin)
and manage.py only after a project is established, it is saved in the root directory of the project.
It can be said that manage.py is a simple package for django-admin.
For more analysis of both, please refer to the official documentation: https://docs.djangoproject.com/en/1.11/ref/django-admin/
MySite
MySite is the management function directory of the project, and the name of this directory varies depending on the name of the project that the user creates, and several files in it are often used to configure the parameters for the entire project.
- settings.py: This file contains initialization settings for the project, which can be configured for the entire project, such as configuring the database, adding applications, and so on.
- urls.py: This is a URL configuration table file that primarily maps URLs to applications, and when a user requests a URL, the Django project points to a target object based on the mapping in the file. The object can be an app's urls.py, or it can be a specific view function. In Django, this file is also called urlconf, which is a very powerful feature of Django.
- wsgi.py: WSGI is the abbreviation for Web Server Gateway interface. WSGI is the server and application standard chosen by Python, and Django will use it. The wsgi.py file defines the projects that we created are WSGI applications. For more information on WSGI see the official documentation: https://wsgi.readthedocs.io/en/latest/index.html
- __pycache__: Starts the service, when browsing the project is generated. No browsing will not be generated, only after the site is running it will appear, it is a compiled folder. There are files that end with. PYc.
Blog
Blog is one of the projects created in the project, with the instructions to create a project can also create a lot of other applications, each creation of an application, Django will be in the root directory of the project to create a subdirectory, and the directory will have the following default files:
The old Ziko Python Django combat note