回顾

模板相关的知识点

与模型不相关的

render做渲染,传递request和前端页面

- devops/urls.py #主路由文件
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^dashboard/', include('dashboard.urls')),
]
- dashboard/urls.py   #路由文件
from django.conf.urls import url,include
from . import views
urlpatterns = [
    url(r'^login/$', views.login),
]
- dashboard/views.py   #视图文件
from django.shortcuts import render
def login(request,*args,**kwargs):
    return render(request,'login.html')
- dashboard/templates/login.html  #模板文件
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ title }}</title>
</head>
<body>
<h1>Login page!!</h1>
</body>
</html>
  • 类视图版本的模板渲染
# 路由
from django.conf.urls import url,include
from . import views
urlpatterns = [
    url(r'^login/$', views.login),
    url(r'^loginview/$', views.LoginView.as_view()),
    url(r'^loginviewv2/$', views.LoginViewV2.as_view()),
]
#类视图版本
from django.views.generic import View,TemplateView
class LoginView(View):
    def get(self,request,*args,**kwargs):
        return render(request,'login.html')
  • 模板视图版本
# 路由
from django.conf.urls import url,include
from . import views
urlpatterns = [
    url(r'^login/$', views.login),
    url(r'^loginview/$', views.LoginView.as_view()),
    url(r'^loginviewv2/$', views.LoginViewV2.as_view()),
]

#模板视图版本
from django.views.generic import View,TemplateView
class LoginViewV2(TemplateView):
    template_name = "login.html"
    def get_context_data(self, **kwargs):
        #接受模板传过来的参数,然后渲染
        kwargs["title"] = "my login"
        return kwargs
    def post(self,request,*args,**kwargs):
        return HttpResponse("")

wanghui
34 声望9 粉丝