Django combat 1- rights management features -04: entrance system

An inlet system

We have created in the project system app, this set of document management capabilities to achieve the rights belong to all systems management. First access system module to achieve an inlet, to the following figure:

1.1 to create a system management views

System management is the view of the entire system module entry, in addition to the introduction of this document is part of Rights Management system management, system configuration, landing logs these common functions can be incorporated into the system management.
Copy sandboxMP / templates / index.html to /sandboxMP/templates/system/system_index.html, and modify system_index.html content:

{% extends "base-left.html" %} {% load staticfiles %} {% block content %} <!-- Main content --> <section class="content"> 系统管理首页:system_index,content是页面定义的主要区域, 头部和底部内容以及导航栏都是通过模板继承的,之后的所有 功能前端页面都是在content内进行编辑。 </section> <!-- /.content --> {% endblock %} {% block javascripts %} {% endblock %}

Writing system management in view SystemView sandboxMP / apps / system / views.py with the following contents:

from django.shortcuts import render
from django.views.generic.base import View

from .mixin import LoginRequiredMixin class SystemView(LoginRequiredMixin, View): def get(self, request): return render(request, 'system/system_index.html') 

1.2 URL Configuration

New sandboxMP / apps / system / urls.py, as follows:

from django.urls import path

from .views import SystemView app_name = 'system' urlpatterns = [ path('', SystemView.as_view(), name='system_index'), ] 

Modify sandboxMP / sandboxMP / urls.py, New:

from django.urls import include

urlpatterns = [
    ...原有内容省略...
    path('system/', include('system.urls', namespace='system')), ] 

This is can be accessed: http://127.0.0.1:8000/system (if the landing will not be redirected to the login page, after landing jumps to / system /)

Knowledge Introduction:
1, the include: urlpatterns may contain other URLconf, when we visit a URL, when Django encounters include, it would remove part of the URL matches, and sends the remaining string to the included URLconf continue process, for example, we access: when / system / users /, and after removing the matching System, the users / string sent system.urls process.
2, namespace: when using the include, can be specified by the namespace namespace parameters instance, if not specified, the default namespace instance URLconf the application name. namespace can be used to reverse lookup the URL, when used in the project we further introduction.

2, expand knowledge: Basic view class

Project began, we have been using the basic view class: View (django.views.generic.base.View), are all of the view class inherits from the View class, which is responsible for connection to view URL, HTTP method scheduling. Further comprising a base view class for RedirectView HTTP redirection, TempateView extend the base class to render template.

Modify sandboxMP / apps / system / views.py TempateView class inheritance in SystemView 1.1 to implement the functions of:

from django.shortcuts import render
from django.views.generic.base import TemplateView

from .mixin import LoginRequiredMixin  class SystemView(LoginRequiredMixin, TemplateView): template_name = 'system/system_index.html' 

SystemView revised inherited TemplateView, no need to rewrite the get () method, you only need to redefine template_name property, to achieve the same functionality 1.1, the code is more concise!

Guess you like

Origin www.cnblogs.com/jameslove/p/10988297.html