Latest web development tutorials

Django create your first project

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

Django management tools

After you install Django, you should now have available management tools we can use to create a project:

We can look at command description:

[root@solar ~]#
Usage: subcommand [options] [args]

  -v VERBOSITY, --verbosity=VERBOSITY
                        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:


Creating your first project use to create the HelloWorld project: startproject HelloWorld

Once created, we can view the directory structure of the project:

[root@solar ~]# cd HelloWorld/
[root@solar HelloWorld]# tree
|-- HelloWorld
|   |--
|   |--
|   |--
|   `--

Directory Description:

  • HelloWorld: container of your project.
  • a useful command-line tool that allows you a variety of ways to interact with this Django project.
  • HelloWorld / __ An empty file that tells Python that directory is a Python package.
  • HelloWorld / Settings of the Django project / configuration.
  • HelloWorld / URL declare the Django project; a drive from the Django website "directory."
  • HelloWorld / a WSGI compliant inlet Web server to run your project.

Next we enter HelloWorld directory, enter the following command to start the server:

python runserver allow other computers can connect to the development server, port number 8000. Without explanation, the default port number 8000.

In your browser and enter your server's ip and port number, if the normal start, the output results are as follows:


View and configure URL

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

from django.http import HttpResponse

def hello(request):
	return HttpResponse("Hello world ! ")

Next, the binding URL and view functions. Open file, delete the original code, copy the following code into file:

from django.conf.urls import *
from HelloWorld.view import hello

urlpatterns = patterns("",
	('^hello/$', hello),

The entire directory structure is as follows:

[root@solar HelloWorld]# tree
|-- HelloWorld
|   |--
|   |-- __init__.pyc
|   |--
|   |-- settings.pyc
|   |--              # url 配置
|   |-- urls.pyc
|   |--              # 添加的视图文件
|   |-- view.pyc             # 编译后的视图文件
|   |--
|   `-- wsgi.pyc

When finished, start the Django development server, open a browser and access the browser and go to:


Note: If the code in the project there are changes, the server will automatically monitor changes to the code and automatically reloads, so if you have started the server is not required to manually restart.