我怎样才能使这种“基于类的视图”起作用?

Posted

技术标签:

【中文标题】我怎样才能使这种“基于类的视图”起作用?【英文标题】:How can I make this "class-based view" work? 【发布时间】:2020-10-27 06:26:14 【问题描述】:

我一直在学习 Django,现在正在研究基于类的视图。我完全按照官方文档和初学者手册中提到的那样输入,但是代码并没有像他们解释的那样工作。请教我为什么这不起作用。

命令提示符显示“文件“C:\Users\yuto1\Desktop\book_study\Tabelog\urls.py”,第 9 行,在 path("list/", ReviewList.as_view(), name="ReviewList"), AttributeError: 'function' 对象没有属性 'as_view'"

urls.py

from django.urls import path,include
from Tabelog import views
from Tabelog.views import ReviewList

app_name = "Tabelog"

urlpatterns = [
    path("lp/", views.lp,name="lp"),
    path("list/", ReviewList.as_view(), name="ReviewList"),
    path("detail/<str:store_name>",views.detail,name="detail"),
    path("form/",views.form,name="form"),
    path("form/fix/<str:pk>",views.form_fix,name="form_fix")
]

views.py

from django.shortcuts import render,redirect,get_object_or_404
from Tabelog.models import Tabelog
from Tabelog.forms import CreateTabelogForm
from django.views.generic import ListView

# Create your views here.
def lp(request):
    return render(request,"Tabelog/lp.html")

def ReviewList(ListView):
    model = Tabelog

def detail(request,store_name):
    detail = Tabelog.objects.get(store_name = store_name)
    context = 
        "detail":detail,
    
    return render(request,"Tabelog/detail.html",context)

def form(request):
    if request.method == "GET":
        form = CreateTabelogForm()
        context = 
            "form":form
        
        return render(request,"Tabelog/form.html",context)

    else:
        form = CreateTabelogForm(request.POST or None)
        if request.method == "POST" and form.is_valid():
            form = CreateTabelogForm(request.POST)
            form.save(commit=True)
            return redirect("Tabelog:list")

        else:
            form = CreateTabelogForm()
            context = 
                "form":form
            
            return render(request,"Tabelog/form.html",context)

def form_fix(request,pk):
    review = get_object_or_404(Tabelog,pk=pk)
    print("success one")
    form = CreateTabelogForm(request.POST or None,instance=review)
    print("success two")

    if request.method == "GET":
        form = CreateTabelogForm(instance=review)
        context = 
            "form":form
        
        return render(request,"Tabelog/form_fix.html",context)

    else:
        if request.method == "POST" and form.is_valid():
            form.save()
            return redirect("Tabelog:list")

        else:
            form = CreateTabelogForm(instance=review)
            context = 
                "form":form
            
            return render(request,"Tabelog/form_fix.html",context)

reviewlist_list.html

<!DOCTYPE html>
% extends 'diary/base.html' %
% block content %
% for contents in object_list%
<article class="store">
  <h1> <a href="% url 'Tabelog:detail' contents.store_name %">contents.store_name</a></h1>
  <h2>contents.get_stars_display</h2>
  <span>contents.outline</span>
</article>
% endfor %
% endblock %

【问题讨论】:

【参考方案1】:

在使用基于类的视图时,您需要通过关键字 class 而不是 def 指定类名(它用于基于函数的视图),如下所示在代码中进行更改。

class ReviewList(ListView):
    model = Tabelog

【讨论】:

以上是关于我怎样才能使这种“基于类的视图”起作用?的主要内容,如果未能解决你的问题,请参考以下文章

按函数名称的 Django 反向基于类的视图不起作用

带有参数的 Django 反向 url 到基于类的视图

向基于类的视图添加额外内容并对其进行过滤

如何动画视图插入/删除的过渡?插入动画不起作用

如何使基于类的视图接受来自 URL 的参数或在 URLconf 中硬编码

我怎样才能使我的菜单栏居中?