我一直在想,get_queryset()在django中是否是多余的。正如我们所知道的,get_queryset()将对象列表返回到页面,但是要返回这个对象列表,我们还可以在get_context_data()中指定它,get_context_data()可以返回更多的变量,而不仅仅是一个列表。一个例子可以看出如下:
返回由get_queryset()编写的儿童图书列表
from django.views.generic import ListView
from books.models import Book
class ChildrenBook(ListView):
mo
这是一个令人尴尬的简单问题。我正在尝试理解如何在我正在构建的第一个Django应用程序中集成一个简单的Python函数。这是我的views.py文件...
from django.shortcuts import render
from noticeboard.models import Listings
from django.views import generic
from django.template import Context
class IndexView(generic.ListView):
template_name = 'listings/index.h
我知道pdb内置了用于调试Python程序和脚本的Python库。但是,您不能真正地使用它来调试Django应用程序(当我尝试在views.py中使用它时会出错)。当Django的跟踪没有帮助时,我可以使用什么工具?
编辑:
from .forms import TestCaseForm, TestCaseSuiteForm
from .models import TestCase, TestSuite
from django.contrib.auth.forms import UserCreationForm
from django.views.generic import FormView
ok民意测验教程已经完成,可以输入问题、投票等,但是从管理页面查看站点,也就是localhost:8000给予.
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
dose/
admin/
The empty path didn’t match any of these.
现在我有民意测验了.
fr
我写了Django应用程序,现在我正试图用自动化测试来覆盖它。为了在我的ListView中测试get_queryset函数,我创建了一个测试用户和他的帖子,但我的测试失败了,"No User matches the given query。当我执行py manage.py runserver时,一切都很好,没有异常,页面显示正常。我对Django测试是新手,所以我完全不知道发生了什么。你能帮我吗?
这是我在view.py上的观点
class UserPostListView(ListView):
"""Displaying a page with a certa
我在连接Django视图和Django模板时遇到了问题。我尝试在FormMixin和ListView中使用基于类的视图。
class Merchants(FormMixin, ListView):
"""
A view of that shows a list of all the merchants.
"""
template_name = "reporting/merchants.html"
model = models.Merchant
context_object_name
我指的是的这一页来编写视图。有人能解释我做错了什么吗?还有什么可以解决的
self.object_list = self.get_queryset()
File "/vagrant/projects/kodeworms/course/views.py", line 23, in get_queryset
self.Course = get_object_or_404(Course, name=self.args[0])
IndexError: tuple index out of range
我的views.py文件
# Create your view
我尝试在django 1.8中使用"add_error“函数。然后我得到了“没有add_error属性”的错误。提前感谢您的帮助。
views.py
class FinalView(ListView):
context_object_name = 'XXX'
template_name = 'XXX.html'
model = Final
def get_queryset(self):
form = InputForm(self.request.GET)
if form.i
我试图创建一个web应用程序,在这个应用程序中,用户可以在更新帖子时看到其他帖子。因此,我想在同一个模板中同时使用ListView和UpdateView。
我的Views.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.c
嗨,我是python的新手,当我在python项目中添加注释选项时。
我想出了一个UnboundLocalError。
我的views.py :
from .forms import PostForm, CommentForm
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.http import Http404
from django.views impor
我是Python的新手,正在尝试理解Django 1.3的基于类的泛型视图。现在,我有以下视图,它获取Category中的Location对象的列表:
class category_detail(ListView):
"""Return a generic view of locations in a category."""
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context.
我有一个UserPassesTestMixin的问题,我想限制查看分配给经理的人员的视图。它给我一个错误,'teamview‘对象没有'get_ object’属性。
下面是我正在讨论的views.py:
from django.shortcuts import render
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.views.generic import ListView, CreateView, DetailView
from .mo
我有一些我写的代码,它可以很好地实现它的目的。
from django.shortcuts import get_object_or_404
from django.views.generic import ListView
from cab.models import Language
class LanguageDetail(ListView):
def get_queryset(self):
slug = self.kwargs['slug']
language = get_object_or_404(Language, sl
我检查了这个网站上的所有问题和答案,但没有找到解决方案。我是Django的新手,正在尝试开发一个存储模型,并且已经在ListView中使用过了。
我的view.py的一部分:
from django.shortcuts import render, redirect
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from Stor
(注意:即使不认识Django,也可能是查询需要工作,所以您可能不需要知道Django/Python)
我有一个ListView,它在pizza表和topping表之间显示映射表中的所有条目。不幸的是,我不能仅仅使用Django的帮助,只给它使用的模型(为了简洁起见,我跳过了为什么),而不得不使用get_queryset。不幸的是,加载时间很慢--表中有将近700个比萨,我需要在sql查询中使用它来按比萨饼名订购。以下是我目前正在做的事情:
class MapPizzaToppingListView(ListView):
model = MapPizzaTopping
pagi
我尝试使用django的CreateView,但得到以下错误: File "/Users/PycharmProjects/gusta/venv/lib/python3.8/site-packages/django/views/generic/edit.py", line 33, in get_form
return form_class(**self.get_form_kwargs())
TypeError: 'str' object is not callable views.py: from django.views.generic import
我看到的不是整个表单,而是提交按钮,而不是表单字段。
目标:让表单字段加载并能够提交表单。
views.py文件
from django.shortcuts import render
from django.template.response import TemplateResponse
from django.views import generic
from friendsbook.models import Status,User
from django.views.generic.edit import CreateView
from .forms import CreatePos
通用视图的存在是为了让我们的生活更轻松,但花在理解这些东西如何工作上的时间实际上让它们变得更加困难。也许是我的问题,但我已经尝试了很长时间来解决这个问题,我可以很容易地自己编写视图,然后继续前进,但我坚持要学习它。
我想要显示一个自定义的DetailView类,代码抛出:
'Sculpture' object has no attribute 'filter'
from django.shortcuts import render, get_object_or_404
from django.views.generic import ListView, Deta
在Django官方教程中,引入了“泛型视图”。
我们有:
(...)
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_poll_list'
def get_queryset(self):
"""Return the last five published polls."""