Building a Python web framework from scratch - Django (1)

Django is an open-source, mvc-mode web application framework under python. We all know that the python version is not backward compatible. The following table shows the correspondence between the Django version and the python version:

 

Django version Python versions
1.8 2.7, 3.2 (until the end of 2016), 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.4, 3.5, 3.6
2.1 3.5, 3.6, 3.7

 

 Below I will describe how to build a web application with Django from scratch.

Step ↓

  1. Install the python environment (not introduced here), the 2.7 version installed by the author.
  2. Download the Django source code, link: https://www.djangoproject.com/download/  , the 1.11.10 version downloaded here. Unzip -> enter the folder -> execute python setup.py install to install. (Django is located in the site-packages directory of the Python installation directory after the installation is successful). If it is a linux environment, execute the following command:
    tar xzvf Django-1.11.10.tar.gz
    cd Django-1.11.10
    python setup.py install
     
  3. verify:
    C:\Users\du>python
    Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on wi
    n32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import django
    >>> django.get_version()
    '1.11.10'
    >>>
    
     The above shows that the installation was successful.
  4. Add the script directory of the python installation directory to the environment variable, such as: D:\Program Files\WinPython-64bit-2.7.10.3\python-2.7.10.amd64\Scripts
  5. Build a project: Enter the path where you want to store the project and execute the following command:
    django-admin.py startproject HelloWorld
     You can see that the project has been generated in the current directory.
    #if windows environment
    django-admin startproject HelloWorld
     
  6. Start the web service: Enter the project HelloWorld root directory and execute:
    python manage.py runserver
     Display information:
    Starting development server at http://127.0.0.1:8000/
    Quit the server with CTRL-BREAK.
     The service starts successfully, open the browser to visit http://127.0.0.1:8000/, and successfully enter the Django main page.

 

At this point, a Django project has been built, and the directory structure is as follows:

|-- HelloWorld
|   |-- __init__.py
|   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py
`-- manage.py

 

  • HelloWorld: The container for the project. 
  • manage.py: A useful command-line tool that lets you interact with this Django project in various ways. 
  • HelloWorld/__init__.py: An empty file that tells Python that this directory is a Python package. 
  • HelloWorld/settings.py: Settings/configuration for this Django project. 
  • HelloWorld/urls.py: The URL declaration for this Django project; a "directory" of websites powered by Django. 
  • HelloWorld/wsgi.py: The entry point for a WSGI compatible web server to run your project. 

The following command can start the service:

python manage.py runserver 0.0.0.0:8000

 The following will introduce how to use the Django framework to develop your own web applications↓

  1.  Create a new view.py file in the HelloWorld directory under the HelloWorld directory, and enter the code:
    from django.http import HttpResponse
     
    def hello(request):
        return HttpResponse("Hello world ! ")
     
  2. 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:
    from django.conf.urls import url
     
    from . import view
     
    urlpatterns = [
        url(r'^$', view.hello),
    ]
     The directory structure is now as follows:
    |-- 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
     
  3. Browser access http://127.0.0.1:8000/, you can see the printed Hello world!
  4.  Modify urls.py:
    from django.conf.urls import url
     
    from . import view
     
    urlpatterns = [
        url(r'^hello$', view.hello),
    ]
     At this point, the browser opens http://127.0.0.1:8000/hello, and you can see the printed Hello world!
The url() function
Django url() can receive four parameters, which are two mandatory parameters: regex, view and two optional parameters: kwargs, name. Next, these four parameters are described in detail.
  • regex: Regular expression, the matching URL will execute the corresponding second parameter view.
  • view: Used to perform URL requests that match a regular expression.
  • kwargs: dictionary-type arguments used by the view.
  • name: Used to get the URL in reverse.

This chapter is written here first!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326201023&siteId=291194637
Recommended