- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为模型查询集创建一个 list_view。运行我的服务器时,它返回:属性错误-“函数”对象没有属性“as_view”。我将不胜感激帮助我解决这个问题。
这是我的代码:
View .py:
@login_required
class live_bids(ListView):
model = Post
template_name = 'loggedin_load/live_bids.html'
def get_queryset(self):
return Post.objects.all().prefetch_related('bids').filter(user=self.request.user)
url(r'^live_bids/$', live_bids.as_view()),
最佳答案
您不能使用 login_required
像这样的类的装饰器。您需要使用 method_decorator
.在 Django 1.9+ 上,您可以装饰类:
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
@method_decorator(login_required, name='dispatch')
class LiveBids(LoginRequiredMixin, ListView):
...
dispatch
并使用
method_decorator
那里。
class LiveBids(LoginRequiredMixin, ListView):
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(LiveBids, self).dispatch(*args, **kwargs)
LoginRequiredMixin
而不是装饰器(适用于 Django 1.9+)
from django.contrib.auth.mixins import LoginRequiredMixin
class LiveBids(LoginRequiredMixin, ListView):
model = Post
template_name = 'loggedin_load/live_bids.html'
def get_queryset(self):
return Post.objects.all().prefetch_related('bids').filter(user=self.request.user)
LiveBids
, 以匹配推荐的 Django 风格。您还必须更新 url 模式。
关于Django/python : 'function' object has no attribute 'as_view' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38724387/
Example views.py: from django.http import HttpResponse from django.views import View class MyView(Vi
我收到一个错误。 我不知道如何修复它,看看,这就是错误: TypeError: as_view() takes 1 positional argument but 2 were given 正如您所看
我正在尝试使用基于类的 View ,但出现了一个奇怪的错误。我使用 View 的方式似乎是正常方式: 成分/模型.py: from django.db import models from djang
我正在尝试在我的项目中引入基于类的 View 。到目前为止看起来还不错,直到发现以下问题。 我正在使用django-navigation创建面包屑。它是这样工作的:装饰了一个 View 函数,并且这个
我有一个 url 通过 as_view 传递参数功能: url( r'^$', ChangeListView.as_view( columns=('username',
以下是我如何设置 FormWizard 的片段。当我点击这个 View 函数时,'bar' 被打印一次,'foo' 被打印 7 次: # views.py def _show_repair_item_
我想更新我的用户配置文件数据。但是运行时出现此错误“function”对象没有属性“as_view”。 我使用的是 Django 1.8,python 2.7 我是 Django 新手。 urls.p
这更像是一个一般的 Python 问题,而不是 Flask 问题。 这段代码来自https://github.com/mitsuhiko/flask/blob/master/flask/views.p
我写了一个 flask ,试图通过使用带有命名空间的蓝图来组织它,遵循这个 tutorial 我遇到了一些问题,浏览了互联网并在 1 中查看了解决方案和 2 .第一个与我所做的无关,第二个解决方案无法
Django 的基于类 View 的 as_view() 语法一直困扰着我。基本上,我厌倦了必须为每个我想使用的基于类的 View 执行 my_view = MyView.as_view() (或者将
我正在尝试使用 TemplateView.as_view()在 urls.py然后使用 ye olde templatetag 命名它 url .这应该工作吗?还是我只是做错了..?还是我的应用程序中
我正在尝试为模型查询集创建一个 list_view。运行我的服务器时,它返回:属性错误-“函数”对象没有属性“as_view”。我将不胜感激帮助我解决这个问题。 这是我的代码: View .py: @
我已经尝试了一段时间,以使用Django Rest Framework使ModelResource或View工作。我正在跟踪示例,但是示例中的代码对我不起作用。谁能告诉我为什么我会收到这个错误。 vi
我正在使用 Flask 框架和 Flask-RESTful 插件编写 RESTful API。我在此插件提供的 Resource 类之上定义了我的 API 类,如 examples 中所示。 。但是,
我正在使用 Flask Restful,并且我想在根端点上渲染 HTML 文件: from flask_restful import Resource, Api app = Flask( __name
我有意见 class CustomSearchView(SearchView): template_name = "bunkering/search.html" queryset =
这是我的观点(简化): class MyView(TemplateView): def __init__(self): self.foo = 'bar' sup
我正在关注 this tutorial ,尝试为我的 Products 表创建一个 API。 这是我的 .views/API/apitest.py View : from my_app.views.A
example中的代码: posts.add_url_rule('/', view_func=ListView.as_view('list')) posts.add_url_rule('//', vi
from django.contrib.auth import views as authViews 是那个模块 我的代码 path("user/", authViews.LoginView.as_v
我是一名优秀的程序员,十分优秀!