Django 2.1.7 Creating a project

13423234-c8e484b60d8cd3cc.png

references

Django 2.2 official documents

Monitoring recent plan to write a class management system based on the latest version of django, to the project as an example to explain how to create a django project.

In the category management system monitoring, operation and maintenance for the development of this there must be a lot of functional modules, such as asset management, middleware monitoring (memcached, redis, etc.), as well as remote call ansible batch processing module, and so on.

So how many modules should be split to write it?

In django, the tissue structure of the project is a project comprises a plurality of applications, an application corresponding to a service module. In other words, the above said plurality of functional modules that can separate multiple applications to write one by one, one by one break.

Install Django 2.1.7

pip3 install django==2.1.7

Examples

Create a project named mysite, complete the "assets - middleware" maintenance information to create the application name assetinfo.

Create a project

django-admin startproject Project Name

$ django-admin startproject mysite

$ ls mysite/
manage.py*  mysite/

I can see has been created mysite project directory. Use pycharm open the project.

13423234-5beaaee5f7725487.png

We can see a lot of projects have been automatically created a directory in which there exists a mysite folder.

Usefulness of these directories and files are:

  • The outermost layer of mysite/the root container just your project, Django does not care about its name, you can rename it to any name you like.
  • manage.py: A variety of ways to let you use the command-line tool Django project management. You can read the django-admin and manage.py get all manage.pythe details.
  • The inner layer of mysite/the directory that contains your project, it is a pure Python package. Its name is that when you refer to it anything inside Python package name to use. (For example mysite.urls).
  • mysite/__init__.py: An empty file that tells Python that this directory should be considered a Python package. If you are a Python beginner, read the official document more about the package .
  • mysite/settings.py: Django project configuration files. If you want to know how this file works, please see the Django settings for details.
  • mysite/urls.py: URL statement Django project, as you site "directory." Read the URL scheduler documentation for more information about URL.
  • mysite/wsgi.py: As you run the project on the entrance WSGI-compliant Web server. Read how to use WSGI deployment for more details.

Simple server for development

Let's confirm whether you really create a Django project a success. If the outer layer of your current directory is not mysitea directory, then switch to this directory, and then run the following command:

$ python3 manage.py runserver

Here if we do not urls.pymake changes, the error will be started directly. See error Django 2.1.7 runserver to start direct error .

First, we need to modify urls.pythe file code is as follows:

from django.contrib import admin
from django.urls import include, path # 增加导入include方法

urlpatterns = [
    path('admin/', admin.site.urls),
]

And then start the service again developed as follows:

13423234-e0153ed20956e1e2.png

Creating an application

Use an application to develop a service module to create applications for the name here assetinfo, complete assets - Information middleware maintenance.

Creating an application command is as follows:
python3 manage.py startapp assetinfo

13423234-f79f54cdb7d1dd32.png

It can be seen after the execution of the command, you create a file name of the application folder and automatically generate the relevant documents, the following files are as follows:

  • _init.py_Is an empty file, the current directory assetinfo can be used as a python packages.
  • tests.pyDocument for the development of test cases, there will be a dedicated testers in the actual development, the things we do not need.
  • models.pyFiles associated with database operations.
  • views.pyFile with the browser request is received, processed, return to the relevant page.
  • admin.pyDocument management with relevant background site.
  • migrationsAfter the folder to introduce to you.

Writing your first view

Let us begin to write your first view of it. Open assetinfo / views.py, the following code is entered into the Python:

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the assetinfo index.")
13423234-cc1a11bb0c29b74a.png

This is the most simple Django view. If you want to see results, we need to map a URL to it - that's why we need URLconf of the.

To create a URLconf, create a new file in urls.py assetinfo directory. Your application directory should now look like this:

13423234-f6c0eae27023fdb8.png

In assetinfo /urls.py, enter the following code:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]
13423234-09235a284edef171.png

The next step is to specify assetinfo.urls module we created in the root URLconf file. Insert a include () in urlpatterns list test1 / urls.py file, the following:

from django.contrib import admin
from django.urls import include, path # 增加导入include方法

urlpatterns = [
    path('assetinfo/', include('assetinfo.urls')), # 导入assetinfo应用的urls.py
    path('admin/', admin.site.urls),
]
13423234-c41a024da0361c65.png

Function include()allows a reference to other URLconfs. Whenever Django encountered: func: <cite> ~ django.urls.include </ cite> , it truncates the URL matches this portion, and to transmit the remaining string URLconf for further processing.

Design include()philosophy is that it can plug and play. Because the application has its own the URLconf ( assetinfo/urls.py), they can be placed in "/ assetinfo /", "/ fun_assetinfo /", "/ content / assetinfo /", or any other path, the applications to work properly.

Now add the index view into the URLconf. You can verify is working properly, run the following command:

python3 manage.py runserver

Use your browser to access HTTP: // localhost: 8000 / assetinfo / , you should be able to see " the Hello, world by You're AT at The polls index.. ", This is your indexdefinition of the view.

13423234-08ceb8661392c8db.png

Start to develop web services

During the development phase, in order to be able to quickly preview the effect of development, django provides a pure python written lightweight web server, used only in the development stage.

Run the server command is as follows:

python3 manage.py runserver ip:port
例:
python3 manage.py runserver

You can not write IP and port, the default IP is 127.0.0.1, the default port of 8000.

13423234-0e3934319aa622f6.png

Reproduced in: https: //www.jianshu.com/p/e690de75d48b

Guess you like

Origin blog.csdn.net/weixin_34255055/article/details/91274429