Django:如何在一个temp中使用两个以上基于类的泛型视图

2024-04-27 03:40:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个web应用程序,用户可以在更新帖子时看到其他帖子。因此,我想在同一个模板中同时使用ListView和UpdateView。你知道吗

我的视图.py地址:

from django.shortcuts import render
from .models import Entry
from django.views.generic import ListView
from django.views.generic.edit import UpdateView
from django.contrib.auth.models import User
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin


class index(LoginRequiredMixin, ListView):
    template_name = 'diary/index.html'
    context_object_name = 'entries'

    def get_queryset(self):  # def get_queryset(self, request):
        return Entry.objects.filter(author=self.request.user)


class EntryUpdate(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = Entry
    fields = ['title', 'content']

    template_name = 'diary/update.html'

    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)

    def test_func(self):
        post = self.get_object()
        if self.request.user == post.author:
            return True
        else:
            return False

我不知道我是否应该创建另一个视图,或者有一个内置的功能,所以,如果你们能帮助我,这将是非常有帮助的。你知道吗

任何帮助都将不胜感激!你知道吗

编辑:

我的ListView代码视图.py地址:

class index(LoginRequiredMixin, ListView):
    template_name = 'diary/index.html'
    context_object_name = 'entries'

    def get_queryset(self):
        return Entry.objects.filter(author=self.request.user)

我的更新视图视图.py地址:

class EntryUpdate(LoginRequiredMixin, MultipleObjectMixin,UserPassesTestMixin, UpdateView):
    model = Entry
    fields = ['title', 'content']
    template_name = 'diary/update.html'

    def get_queryset(self):
        return Entry.objects.filter(author=self.request.user)

    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)

    def test_func(self):
        post = self.get_object()
        if self.request.user == post.author:
            return True
        else:
            return False

错误,我得到:

'EntryUpdate' object has no attribute 'object_list'

Tags: namefromimportselfform视图getreturn
1条回答
网友
1楼 · 发布于 2024-04-27 03:40:02

您可以在UpdateView中尝试MultipleObjectMixin。你知道吗

您可以使用此mixin get_queryset()定义并访问模板中的object_list。查看the documentation了解更多信息

编辑

当然,这里有一个简短的代码示例:

# demo/models.py

from django.db import models

class Title(models.Model):
    title = models.CharField(max_length=100)
# demo/views.py

from django.views.generic import UpdateView
from django.views.generic.list import MultipleObjectMixin
from demo.models import Title


class UpdateWithListView(UpdateView, MultipleObjectMixin):
    model = Title
    template_name_suffix = '_update_form_with_list'
    fields = ['title']
    object_list = Title.objects.all()

update_with_list_view = UpdateWithListView.as_view()
# my_project/urls.py

from django.contrib import admin
from django.urls import path
from demo.views import update_with_list_view

urlpatterns = [
    path('<int:pk>', update_with_list_view),
    path('admin/', admin.site.urls),
]

以及模板:

demo/templates/demo/title_update_form_with_list.html


Current title: {{ object.title }}

<form method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Update">
</form>

<p>All other titles:</p>

{% for title in object_list %}
<p>Title:  {{ title.title }}</p>
{% endfor %}

这就是我在模板中得到的(我的数据库中有10个“标题”,每个标题都带有随机字符):

UpdateView with MultipleObjectMixin

编辑2 关于您编辑的问题,您的视图中缺少“object\u list”的定义,这是MultipleObjectMixin所要求的。你知道吗

请注意,在我的代码示例views.py中,我用填充object_list的查询定义了object_list。我相信您收到的错误是因为mixin希望收到object_list。你知道吗

请尝试添加:


# demo/views.py

# omitted imports


class UpdateWithListView(UpdateView, MultipleObjectMixin):
    model = Title
    template_name_suffix = '_update_form_with_list'
    fields = ['title']
    object_list = Title.objects.all() # make sure to define this with your query

update_with_list_view = UpdateWithListView.as_view()

如果我没弄错的话,get_queryset()方法负责UpdateView的对象检索,而object_listListView相关。你知道吗

请尝试将object_list添加到您的视图中,并检查它是否解决了问题。你知道吗

相关问题 更多 >