• django CBV基于类视图简单实例


    URLS:

    from django.contrib import admin
    from django.urls import path
    from cmbd import views
    
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('home/', views.Home.as_view()),
    ]
    

    VIEWS:

    from django.shortcuts import render,redirect
    from django.shortcuts import HttpResponse
    # Create your views here.
    from django.views import View
    class Home(View):
        def dispatch(self, request, *args, **kwargs):
            print('before')
            result = super(Home,self).dispatch(request,*args,**kwargs)
            print('after')
            return result
        def get(self,request):
            print('in get')
            return render(request,'home.html')
        def post(self,request):
            print('in post')
            return render(request,'home.html')
    

    TEMPLATES下home.html:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <div>
            <form action="/home/" method="post">
                <input type="text" name="user" />
                <input type="submit" value="submit" />
            </form>
        </div>
    </body>
    </html>
    

      

  • 相关阅读:
    202. Happy Number
    198. House Robber
    191. Number of 1 Bits
    190. Reverse Bits
    189. Rotate Array
    172. Factorial Trailing Zeroes
    171. Excel Sheet Column Number
    [leetcode]Single Number II
    [leetcode]Single Number
    [leetcode]Clone Graph
  • 原文地址:https://www.cnblogs.com/alex-hrg/p/9671436.html
Copyright © 2020-2023  润新知