Django creates the first project

Django creates the first project

In this chapter we will introduce the Django management tools and how to use Django to create the project, the first project we use HelloWorld to order the project.

Test Release Notes:

Python 2.7.10
Django 1.10.6

Django management tools

After installing Django, you should now have the available administrative tools We can use to create a project:
We can look at the following command description:
[root@solar ~]#
Usage: subcommand [options] [args]

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 will be
A directory to add to the Python path, e.g.
--traceback Raise on exception
--version show program'
s version number and exit
-h, --help show this help message and exit

Type ' help <subcommand>' for help on a specific subcommand.

Available subcommands:

……abbreviated part……
 Create the first project
Use to create a HelloWorld project startproject HelloWorld
After the creation is complete, we can view the directory structure of the next project:
Catalog Description:
HelloWorld: container of the project. A useful command-line tool that allows you to interact with the Django project in a variety of ways.
HelloWorld / __ An empty file that tells Python that the directory is a Python package.
HelloWorld / The settings / configuration of the Django project.
HelloWorld / the Django project URL declaration; a Django-driven site “directory”.
HelloWorld / A WSGI-compliant Web server portal to run your project.
Next we enter the HelloWorld directory and enter the following command to start the server:
python runserver to other computers can be connected to the development server, 8000 for the port number. If not, then the port number defaults to 8000.
In the browser input your server ip and port number, if the normal start, the output is as follows:

View and URL configuration

Create a new file in the HelloWorld directory under the previously created HelloWorld directory and enter the code:
HelloWorld / HelloWorld / file code:

from django.http import HttpResponse def hello(request): return HttpResponse(Hello world ! )

Next, bind the URL with the view function. Open the file, delete the original code, copy and paste the following code into the file:
HelloWorld / HelloWorld / File Code:

from django.conf.urls import url from . import view urlpatterns = [ url(r^$, view.hello), ]
The entire directory structure is as follows:
$ tree
|– HelloWorld
|   |–
|   |– __init__.pyc
|   |–
|   |– settings.pyc
|   |–              # url deploy
|   |– urls.pyc
|   |–              # Added view file
|   |– view.pyc             # Compiled view file
|   |–
|   `– wsgi.pyc
When finished, start the Django development server and access the browser in the browser and access:

We can also modify the following rules:

HelloWorld/HelloWorld/ File code:

from django.conf.urls import url

from . import view

urlpatterns = [ url(r^hello$, view.hello),


Through the browser to open, the output is as follows

Note: If the code changes in the project, the server will automatically monitor the code changes and automatically reload, so if you have started the server without a manual restart.

Url () function

Django url () can receive four parameters, are two mandatory parameters: regex, view and two optional parameters: kwargs, name, then the details of these four parameters.

Regex: regular expression, matching the URL will perform the corresponding second parameter view.

View: Used to execute a URL request that matches a regular expression.

Kwargs: parameters of the dictionary type used by the view.

Name: used to reverse the URL.

Leave a Reply

Your email address will not be published. Required fields are marked *