Create a Django application and routing configuration

Django Description: web application framework is an open source, written in python. Initial release in July 2005, and released the first official version 1.0 in September 2008.

File Description:
manage.py: A command-line tool that allows us to use a variety of ways to interact with the Django project.
project: __init__.py: An empty file that tells the python directory should be seen as a package
settings.py: Project profile
urls.py: URL statement engineering (URL primary controller)
wsgi.py: Engineering and WSGI Wing entrance of the web server
configuration database: Note: Django default SQLite database
in settings.py file, the database is configured by dATABASES, the option
to start the service: python manage.py runserver

 

1.dj create sub-applications

 

 

 

 

2. Create a complete sub-applications, but also to register before you can

1. Locate the file settings.py

 

2. Registration sub-applications

 

 

Note: In fact, the import is ProjectsConfig class of apps

 

 

 

3. Register view

1. Register view inside views.py

from django.http import HttpResponse

# Create your views here.

def index(request):

    """

    The first view function

    :return:

    """

    return HttpResponse ( "<h1> Hello, hello measured open big brother </ h1>")

 

 

 

4. Configure Route

   Global Routing

   1. Import Application

    2. Configure routing

 

 

 

Introducing the view class

from projects.views import index

urlpatterns = [

    path('pro/', index)

]

 

The role of each file created sub-applications 5

1.admin.py equivalent console

2.apps.py be application configuration

3.models.py defined database model, with interaction with the database here

4.test.py unit testing

5.views.py store view

 

6. Create a sub-routing

1. First sub-application which urls.py new folder, used to store routing

2. Then urls folder inside routing configuration

from django.urls import path

from pro_01.views import index_02

urlpatterns = [

    path('', index_02)

]

3. introducing in the global routing inside

from django.urls import  include

Configuring Routing

urlpatterns = [

    path('pro_01/', include('pro_01.urls'))

]

4. Then the routing global configuration, the level will be a route to look for the Sub

 

Summary: 1. Each application module will maintain a sub-route

   2. Like the main routes, but also from top to bottom to match

   3 can match the, path of the second argument specifies the view is executed, not matching, the exception is thrown 404

 

Note: Sub-routing can not be nested route, you can write multi-level sub routing

 

 

View function

 

def index_01(resquest):

    """

    View function, this view all of the requested function supporting method

    : Param resquest: resquest is HttpRequest object, all information including the end user's request

    : Return: must return an HttpResponse object or child objects

    """

    return HttpResponse ( "<h1> Exercise registered view function </ h1>")

 

 

Note: The need to comment MIDDLEWARE inside the settings.py file inside

django.middleware.csrf.CsrfViewMiddleware'

 

If we view function supports multiple request methods, it is not easy to maintain the view function, we can use the class function

Better to create various requests

 

Class function

from django.views import View

class IndexView(View):

 

    def get(self, request):

        return HttpResponse ( "<h1> This is a get request method </ h1>")

 

    def delete(self, request):

        return HttpResponse ( "<h1> This is a get request method </ h1>")

 

    def put(self, request):

        return HttpResponse ( "<h1> This is a get request method </ h1>")

 

    def post(self, request):

        return HttpResponse ( "<h1> This is a get request method </ h1>")

(Note: We can apply alt + enter shortcut to import classes directly write the name of the class you want to import, and shortcut keys on the line)

 

Route sub-view configuration:

1. We need to import the module sub-view application

2, then according to the path inside views. Written class view .as_view () as_view () must be bracketed, or will be error

from pro_01 import views

 

urlpatterns = [

    # path('', index_02)

    path('', views.IndexView.as_view())

]

 

Guess you like

Origin www.cnblogs.com/666666pingzi/p/11809564.html