Showing posts with label Introduction to Django. Show all posts
Showing posts with label Introduction to Django. Show all posts

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 django-admin.py. We can use django-admin.py to create a project:
We can look at the following django-admin.py command description:

[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>' for help on a specific subcommand.

Available subcommands:

[django]
    check
    cleanup
    compilemessages
    createcachetable
……abbreviated part……

 Create the first project
Use django-admin.py to create a HelloWorld project

django-admin.py startproject HelloWorld

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

HelloWorld: container of the project.

Manage.py: A useful command-line tool that allows you to interact with the Django project in a variety of ways.

HelloWorld / __ init__.py: An empty file that tells Python that the directory is a Python package.
HelloWorld / settings.py: The settings / configuration of the Django project.
HelloWorld / urls.py: the Django project URL declaration; a Django-driven site "directory".
HelloWorld / wsgi.py: 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 manage.py runserver 0.0.0.0:8000

0.0.0.0 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 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 with the view function. Open the urls.py file, delete the original code, 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 deploy
|   |-- urls.pyc
|   |-- view.py              # Added view file
|   |-- view.pyc             # Compiled view file
|   |-- wsgi.py
|   `-- wsgi.pyc
`-- manage.py
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/urls.py File code:


from django.conf.urls import url

from . import view

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

 ]

Through the browser to open http://127.0.0.1:8000/hello, 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.

Django Tutorial


Introduction to Django and Installation


There are many different Web frameworks in Python. Django is the most representative of a heavyweight player. Many successful sites and APP are based on Django.

Django is an open source Web application framework written by Python.

Django complies with BSD copyright, which was first released in July 2005 and released its first official version 1.0 in September 2008.

Django uses MVC's software design pattern, model M, view V, and controller C.

Who is suitable for reading this tutorial?

This tutorial is for developers with Python-based developers.

Before you learn this tutorial you need to know

Before you study this tutorial you need to understand some basic Web knowledge and Python 2.x tutorial or Python 3.x tutorials.

Django version of the corresponding version of the Python:

Django version
Python version
1.8
2.7, 3.2 , 3.3, 3.4, 3.5
1.9, 1.10
2.7, 3.4, 3.5
1.11
2.7, 3.4, 3.5, 3.6
2.0
3.5+













How to Install Django:

Before installing Django, the system needed to have a Python development environment. Then we have to look at the specific system under the Django installation.

Window Install Django

If you have not installed the Python environment, you need to download the Python installation package first.

1, Python Download: https://wwwthonthon.org/downloads/

2, Django download address: https://www.djangoproject.com/download/

Note: Currently Django 1.6.x or later is fully compatible with Python 3.x.
Python install (installed skippable)

Install Python You only need to download python-x.x.x.msi file, and then click the "Next" button.


After the installation is complete you need to set the Python environment variable. Right click on the computer -> Properties -> Advanced -> environment variable -> modify the system variable path, add Python installation address, this example is using C: \ Python33, you need to install according to your actual situation.

Django installed

Download the Django archive, extract it and place it in the same root directory as the Python installation directory, go to the Django directory, execute python setup.py install, and start the installation. Django will be installed under Python's Libsite site-packages.



Then configure the environment variables to add these directories to the system environment variable: C: \ Python33 \ Lib \ site-packages \ django; C: \ Python33 \ Scripts. After the addition is complete, you can use Django's django-admin.py command to create a new project.

Check if the installation is successful

Enter the following command to check:

>>> import django
>>> print django.get_version ()



If the Django version number is output, the installation is correct.

Install Django on Linux

Yum installation method

The following installation is installed in the Centos Linux environment, if it is your Linux system is ubuntu Please use the apt-get command.

By default, the Linux environment already supports Python. You can enter the Python command at the terminal to see if it is already installed.

Python 2.7.3 (default, Aug 1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Install setup tools

command:

Yum install setuptools

When you are done, you can use the easy_install command to install django

Easy_install django

Then we enter the following code in the python interpreter:

[Root @ solar django] # python
Python 2.7.3 (default, May 15 2014, 14:49:08)
[GCC 4.8.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.VERSION
(1, 6, 5, 'final', 0)
>>> 

We can see the output Django version number, indicating that the installation is successful.



Popular Posts