This article example describes the method of Django framework to create a project. Shared for your reference, as follows:
Django Administration Tools
After installing Django, you will have a management tool available to you. We can use it to create a project:: .
We can look at the command description below.
Type ' help <subcommand>' for help on a specific 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
Create a project
Use to create a djangoPro project:
startproject djangoPro
For the latest version of Django, please use the django-admin command.
django-admin startproject djangoPro
The directory structure of the project after creation:
$ cd djangoPro/
$ tree
.
|-- HelloWorld
| |-- __init__.py
| |--
| |--
| `--
`--
Catalog Description:
- djangoPro: Container for the project.
- : A useful command line tool that lets you interact with this Django project in a variety of ways.
- djangoPro/__init__.py: An empty file that tells Python that the directory is a Python package.
- djangoPro/: Settings/configuration for this Django project.
- djangoPro/: URL declaration for this Django project; a Django-driven "catalog" of websites.
- djangoPro/: A WSGI-compliant web server entry point to run your project.
Next we go to the djangoPro directory and enter the following command to start the server:
python3 runserver 0.0.0.0:8000
0.0.0.0 allows other computers to connect to the development server. 8000 is the port number. If not specified, the port number defaults to 8000.
Enter the ip and port number of your server in the browser, and if it starts normally, the output will be as follows:
View and URL Configuration
Create a new file in the djangoPro directory under the previously created djangoPro directory and enter the code:
from import HttpResponse def hello(request): return HttpResponse("Hello world ! ")
Next, bind the URL to the view function. Open the file, delete the original code, and copy and paste the following code into the file:
from import url from . import view urlpatterns = [ url(r'^$', ), ]
The following rules can also be modified:
from import url from . import view urlpatterns = [ url(r'^hello$', ), ]
Note: If there are changes to the code in the project, the server will automatically monitor the changes and reload the code automatically, so you don't need to restart the server manually if you have already started it.
url() function
Django url() can receive four parameters, are two mandatory parameters: regex, view and two optional parameters: kwargs, name, the next detailed description of these four parameters.
- regex: Regular expression, matching URLs will be executed with the second parameter view.
- view: Used to execute URL requests that match a regular expression.
- kwargs: Arguments of dictionary type used by the view.
- name: Used to reverse the URL.
I hope that what I have said in this article will help you in designing Python programs based on Django framework.