home PYTHONJAVA
 

Django creates the first project

In this chapter we will introduce Django management tools and how to use Django to create projects. The first project we use HelloWorld to command projects.

Test Release Notes:

  • Python 2.7.10

  • Django 1.10.6


Django Admin Tools

After installing Django, you should now have the available administration tool django-admin.py. We can use django-admin.py to create a project:

We can look at the command of django-admin.py:

[root@solar ~ ]# django-admin.py
Usage: django-admin.py subcommand [options] [args]

Options:
  -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
                        Used.
  --pythonpath=PYTHONPATH
                        A directory to add to the Python path, e.g.
                        "/home/djangoprojects/myproject".
  --traceback Raise on exception
  --version show program's version number and  exit
  -h, --help show this help message and exit
      
      
 Type 'django-admin.py help <subcommand>'< /span> for help on a specific subcommand.

Available subcommands:

[django]
     Check
     Cleanup
     Compilemessages
     Createcachetable
......omit part...

Create the first project

Use django-admin.py to create a HelloWorld project:

django-admin.py startproject HelloWorld

The latest version of Django uses the django-admin command:

django-admin startproject < /span>HelloWorld

After the creation is complete, we can view the directory structure of the next project:

$ cd HelloWorld/< /span>
$ tree
.
|-- HelloWorld
| |-- __init__.py
| |-- settings.py
| |-- urls.py
| `-- wsgi.py
`-- manage.py

Directory description:

  • HelloWorld: The container for the project.
  • Manage.py: A useful command line tool that lets you interact with the Django project in a variety of ways.
  • HelloWorld/__init__.py: An empty file telling Python that the directory is a Python package.
  • HelloWorld/settings.py: The setup/configuration of this Django project.
  • HelloWorld/urls.py: The URL declaration for the Django project; a "directory" for the site driven by Django.
  • HelloWorld/wsgi.py: An entry for a WSGI compliant web server to run your project.

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

python manage.py 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 stated, the port number defaults to 8000.

Enter the ip and port number of your server in the browser. If it starts normally, the output is as follows:

View and URL configuration

Create a new view.py file in the HelloWorld directory under the previously created HelloWorld directory and enter the code:

HelloWorld/HelloWorld/view.py File code:

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

Next, bind the URL to the view function. Open the urls.py file, delete the original code, and copy and paste the following code into the urls.py file:

HelloWorld/HelloWorld/urls.py 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__.py
| |-- __init__.pyc
| |-- settings.py
| |-- settings.pyc
| |-- urls.py # url configuration
| |-- urls.pyc
| |-- view.py # added view file 
| |-- view.pyc # compiled view file
| |-- wsgi.py
| `-- wsgi.pyc
`-- manage.py

When you're done, launch the Django development server and open a browser in your browser access and access:

We can also modify the following rules:

HelloWorld/HelloWorld/urls.py File code:

from django .conf.urls import url from . import view urlpatterns = [ url(r'^hello$', view.hello), ]

Open http://127.0.0.1:8000/hello in your browser and 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 you don't need to manually restart if you have already started the server.


url() function

Django url() can accept four parameters, two mandatory parameters: regex, view, and two optional parameters: kwargs, name, and then detail these four parameters.

  • regex: A regular expression whose matching URL will execute the corresponding second parameter view.

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

  • kwargs: The parameters of the dictionary type used by the view.

  • name: Used to get the URL in reverse.






welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.