Django9 CBV基于类的视图

urls

from django.contrib import admin
from django.urls import path, include

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

App.urls

from django.urls import path

from App import views

app_name = 'App'
urlpatterns = [
    path('', views.index, name='index'),
    path('register/', views.RegisterView.as_view(a=90), name='register'),
    path('template/',views.MyTemplateView.as_view(),name='template'),
    # ListView
    path('list/',views.UserListView.as_view(), name='list'),
    path('detail//',views.UserDetailView.as_view(), name='detail'),
    # 创建用户
    path('create/',views.UserCreateView.as_view(),name='create'),

    # 路由保护
    path('publish/',views.ArticlePulishView.as_view(),name='publish'),
    path('demo/',views.DemoView.as_view(),name='demo')
]

App.views

from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.utils.decorators import method_decorator

from django.views.generic import View

# Create your views here.
# Function Based View  FBV
# 简单,易懂
# 缺点:不能继承,不容易代码复用
from django.views.generic import TemplateView, ListView, DetailView,CreateView

from App.models import User


def index(request):
    return HttpResponse("首页")

# CBV Class Based View 基于类的视图
# 优点:有继承,代码可复用、可维护性更强;一个请求对应一个方法,无需判断
# 缺点:比较抽象,不易懂
class RegisterView(View):
    a = 10
    def get(self,request):
        print(self.a)
        return HttpResponse("GET")

    def post(self,request):
        return HttpResponse("POST")

    def put(self,request):
        return HttpResponse("PUT")

    def delete(self,request):
        return HttpResponse("DELETE")


class MyTemplateView(TemplateView):
    # 模板文件名
    template_name = 'example.html'
    # 获取模板中数据
    def get_context_data(self, **kwargs):
        kwargs = super().get_context_data(**kwargs)
        kwargs['name'] = '意大利'
        kwargs['title'] = "欧洲新冠王者"
        return kwargs


class UserListView(ListView):
    # 模板文件名
    template_name = 'userlist.html'
    paginate_by = 1  # 每页记录个数
    ordering = ("uid",)    # 排序字段列表
    # 查询结果集
    # queryset = User.objects.all()

    # 获取查询数据
    def get_queryset(self):
        data = User.objects.filter(uid__gt=20)
        ordering = self.get_ordering()
        if ordering:
            if isinstance(ordering, str):
                ordering = (ordering,)
            data = data.order_by(*ordering)

        return data


class UserDetailView(DetailView):
    template_name = 'userdetail.html'
    queryset = User.objects.all()
    context_object_name = 'user'


class UserCreateView(CreateView):
    # template_name = 'userregister.html'
    # model = User
    # # 字段列表,用于创建用户时设定用户属性
    # fields = ['username','password']
    # success_url = '/'  # 创建成功后跳转的页面
    def get(self,request):
        return render(request,'userregister.html')
    def post(self,request):
        data = request.POST.dict()
        data.pop('csrfmiddlewaretoken')
        User.objects.create(**data)
        return redirect("/")


def check_login(func):
    def inner(request,*args,**kwargs):
        if request.session.get('usename'):
            return func(request,*args,**kwargs)
        return redirect("/")
    return inner


# dispatch对所有请求方法起作用
@method_decorator(check_login,name='dispatch')
class ArticlePulishView(View):
    def get(self,request):
        return HttpResponse("文章展示")

    # @method_decorator(check_login)
    def post(self,request):
        return HttpResponse("发表文章")
        
        
class DemoView(View):
    def get(self,request):
        object_list = User.objects.all()
        pagitor = Paginator(object_list,10)
        pager = pagitor.page(1)
        object_list = pager.object_list
        return render(request,'userlist.html',locals())

App.models

from django.db import models

class User(models.Model):
    uid = models.AutoField(primary_key=True)
    username = models.CharField(unique=True, max_length=30)
    password = models.CharField(max_length=128)
    regtime = models.DateTimeField(auto_now=True)
    sex = models.IntegerField(blank=True, null=True)
    is_active = models.IntegerField(blank=True, null=True)

    class Meta:
        db_table = 'user'

example.html


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ title }}title>
head>
<body>
<p>
    {{ name }}
p>
body>
html>

userlist.html


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
<table border="1" cellspacing="0" width="80%">
    {% for user in object_list %}
    <tr>
    <td>{{ user.uid }}td>
        <td>{{ user.username }}td>
        <td>{{ user.password }}td>
    tr>
    {% endfor %}
table>
<div>
    {% for current in paginator.page_range %}
        <a href="">{{ current }}a>
    {% endfor %}
div>
body>
html>

userdetail.html


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
<h2>用户信息h2>
{{ user.uid }}  ---{{ user.username }}----{{ user.password }}
body>
html>

userregister.html


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
<form action="{% url 'App:create' %}" method="post">
    {% csrf_token %}
    用户名: <input type="text" name="username"> <br>
    密码: <input type="password" name="password"> <br>
    <input type="submit">
form>
body>
html>

App.tests

class Dog:
    name = '黑背'
    def run(self):
        print("run")


def go():
    print("GO")

dog = Dog()
# print(getattr(dog,'name'))
fun = getattr(dog,'run1',go)
fun()

# print(hasattr(dog,'nam2e'))

你可能感兴趣的:(5-web)