- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个中间模型的模型表单集,我用它来将用户分配给另一个模型。当我尝试生成 View 时,我得到 'StaffAssignmentForm' object has no attribute 'forms'
。
模型
class StaffAssignment(models.Model):
study = models.ForeignKey(Study, related_name='study_set', null=True, on_delete=models.CASCADE)
staff = models.ForeignKey(User, related_name='assigned_to_set', null=True, on_delete=models.CASCADE)
role = models.CharField(max_length=100, null=True)
assigned_on = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ('-role',)
def __str__(self):
return '{} is assigned to {}'.format(self.staff, self.study)
表格
class AddStaff(forms.ModelForm):
model = StaffAssignment
fields = ('staff',)
def __init__(self, *args, **kwargs):
super(AddStaff, self).__init__(*args, **kwargs)
for form in self.forms:
for field in form.fields:
form.fields[field].widget.attrs.update({'class': 'form-control'})
View
def add_staff(request, study_slug):
study = get_object_or_404(Study, slug=study_slug)
staff_formset = modelformset_factory(StaffAssignment, form=AddStaff, fields=('staff',), extra=5)
if request.method == 'POST':
staffList = staff_formset(request.POST, request.FILES)
if staffList.is_valid():
for assignment in staffList:
assigned = assignment.save(commit=False)
assigned.study = study
assigned.save()
return HttpResponseRedirect(reverse('studies:studydashboard'))
else:
pass
else:
staffList = staff_formset(queryset=StaffAssignment.objects.none())
return render(request, 'studies/addstaff.html', {'staffList': staffList, 'study': study})
回溯
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/studies/add-staff/lilly-a4/
Django Version: 1.8.7
Python Version: 3.5.0
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'studies',
'account')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Template error:
In template /Users/RickyD/PycharmProjects/StudyTrack/studies/templates/studies/addstaff.html, error at line 20
'StaffAssignmentForm' object has no attribute 'forms'
10 : {% block content %}
11 : <div class="row">
12 : <div class="col-md-8">
13 : <div class="box box-primary">
14 : <div class="box-header with-border">
15 : <h3 class="box-title">Add new study </h3>
16 : </div>
17 : <form action="{% url 'studies:addstaff' study.slug %}" method="post" enctype="multipart/form-data">
18 : {% csrf_token %}
19 : <div class="box-body">
20 : {{ staffList.as_p }}
21 : </div>
22 : <div class="box-footer">
23 : <button type="submit" class="btn btn-primary">Submit</button>
24 : </div>
25 : </form>
26 : </div>
27 : </div>
28 : </div>
29 :
30 : <div class="col-md-4">
Traceback:
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/RickyD/PycharmProjects/StudyTrack/studies/views.py" in add_staff
87. return render(request, 'studies/addstaff.html', {'staffList': staffList, 'study': study})
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/shortcuts.py" in render
67. template_name, context, request=request, using=using)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/loader.py" in render_to_string
99. return template.render(context, request)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/backends/django.py" in render
74. return self.template.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in render
210. return self._render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in _render
202. return self.nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in render
905. bit = self.render_node(node, context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/loader_tags.py" in render
135. return compiled_parent._render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in _render
202. return self.nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in render
905. bit = self.render_node(node, context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/loader_tags.py" in render
65. result = block.nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in render
905. bit = self.render_node(node, context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/debug.py" in render
89. output = self.filter_expression.resolve(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in resolve
648. obj = self.var.resolve(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in resolve
789. value = self._resolve_lookup(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in _resolve_lookup
849. current = current()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/forms/formsets.py" in as_p
406. forms = ' '.join(form.as_p() for form in self)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/forms/formsets.py" in __iter__
73. return iter(self.forms)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/functional.py" in __get__
59. res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/forms/formsets.py" in forms
142. forms = [self._construct_form(i) for i in range(self.total_form_count())]
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/forms/formsets.py" in <listcomp>
142. forms = [self._construct_form(i) for i in range(self.total_form_count())]
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/forms/models.py" in _construct_form
596. return super(BaseModelFormSet, self)._construct_form(i, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/forms/formsets.py" in _construct_form
167. form = self.form(**defaults)
File "/Users/RickyD/PycharmProjects/StudyTrack/studies/forms.py" in __init__
69. for form in self.forms:
Exception Type: AttributeError at /studies/add-staff/lilly-a4/
Exception Value: 'StaffAssignmentForm' object has no attribute 'forms'
最佳答案
您在表单中迭代表单,但表单没有任何表单属性。
class AddStaff(forms.ModelForm):
model = StaffAssignment
fields = ('staff',)
def __init__(self, *args, **kwargs):
super(AddStaff, self).__init__(*args, **kwargs)
#for form in self.forms:
#for field in form.fields:
for field in self.fields:
self.fields[field].widget.attrs.update({'class': 'form-control'})
关于python - Django 属性错误 : object has not attribute 'forms' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35191209/
关于 this页面,我看到以下代码: if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden) 但我不明白为什么会变成这样。
函数pthread_mutex_init允许您指定指向属性的指针。但是我还没有找到关于pthread属性是什么的很好的解释。我一直只是提供NULL。这个论点有用吗? 该文档,对于那些忘记它的人: PT
我们有一个 xml 节点“item”,其属性为“style”,即“Header1”。但是,这种风格可以改变。我们有一个名为 Header1 的属性集,它定义了它在 PDF 中的外观,通过 xsl:fo
我的任务是在用户点击它时从输入框中删除占位符并使标签可见。如果用户未在其中再次填写任何内容,请放回占位符并使标签不可见。 我可以隐藏它但不能重新分配它。我试过 element.setAttribute
我从文章中编写代码,并且有: public IActionResult Create([Bind(Include="Imie,Nazwisko,Stanowisko,Wiek")] Pracownik
你能给我解释一下以下属性吗? 1) [MonoTouch.Foundation.Register("SomeClass")] 这个属性是否只用于向IB注册类?以编程方式扩展 iOS 类时是否必须使用此
我正在编写一个 C++ 程序,在调试时我在执行以下函数: int CClass::do_something() { ... // I've put a breakpoint here } 我的 C
我已经在 polymer 0.5 中构建了我的应用程序。 现在我已经将它更新到 polymer 1.0。 对于响应式布局,我使用了一个布局属性,它使用 Polymer 0.5 中布局属性的自定义逻辑。
我是使用 Jade 的新手——到目前为止它很棒。 但是我需要发生的一件事是具有“itemscope”属性的元素: 我的 Jade 符是: header(itemscope, itemtype='ht
我正在研究一个厨师实现,有时在过去的地方使用了 attribute.set,attribute.default 会这样做。为了解决这个问题,我对 Chef 属性优先范式非常熟悉。我知道“正常”属性(使
我经常看到html data-attribute (s) 将特定值/参数添加到 html 元素,例如使用它们将按钮“链接”到要打开的模式对话框等的 Bootstrap。 现在,我看到一个几乎著名的
假设如下: def create_new_salt self.salt = self.object_id.to_s + rand.to_s end 为什么使用“ self ”更好。而不是实例变量“
根据我的理解,Backbone.js 模型的属性应该通过以下方式声明为有点私有(private)的成员变量 this.set({ attributeName: attributeValue }) //
我有一个看起来像下面的XML文档: ... ... ... ...
我正在实现一个 JSF 组件,需要有条件地添加一些属性。这个问题类似于之前的 JSF: p:dataTable with f:attribute results in "argument type m
我正在尝试将应用程序发布到 Android 电子市场,但出现以下错误: W/ResourceType(16964): No known package when getting value for r
抱歉这么具体的应用程序,但我注意到另一篇关于 Maya 开发的回答很好的帖子。 我刚刚为 Maya 编写了一个插件节点。它只是根据湍流函数杀死一堆粒子。湍流由许多可在属性编辑器中调整的属性驱动。 在属
我在 html 元素中的数据属性为 Update .它具有数据属性的 bool 值。 跟下面的元素Update有什么区别吗?因为数据属性用双引号引起来。 html是否支持 bool 值? 最佳答案 b
我正在尝试为企业库 5.0 的异常处理 block 创建自定义异常处理程序。据我了解,我需要使用属性开始上课“[ConfigurationElementType(typeof(CustomHandle
我找不到这两个选择器之间的区别。两者似乎都做同样的事情,即根据包含给定字符串的特定属性值选择标签。 对于 [attribute~=value] :http://www.w3schools.com/cs
我是一名优秀的程序员,十分优秀!