python - “str”对象没有属性“as_widget”
问题描述
我在 django 中创建了一个表单并添加了模板标签 Add_attr 问题是我收到这些错误。
'str' object has no attribute 'as_widget'
至
{{ form.session_type|add_class='form-control' }}
模板标签:
from django import template
register = template.Library()
@register.filter(name='add_attr')
def add_attr(value, arg):
return value.as_widget(attrs={'class': arg})
编辑会话.html
<div class="form-group">
<label for="exampleInputEmail2">Session Type</label>
<span class="input-icon icon-right">
{{form.session_type|add_attr:"class=form-control"}}
</span>
</div>
视图.py
class sessionpdate_view(UpdateView):
model = Session
fields = '__all__'
template_name = 'myapp/editSession.html'
success_url = '/sessionDetail/'
解决方案
推荐阅读
- azure - Azure Repo - 如何将标签组织到文件夹中
- json - 使用 jq 将字符串列表转换为 JSON 数组
- python-2.7 - 在 ubuntu 18.04 中使用 python 2.7 构建错误 Buildout Plone 4.3.11
- spring-boot - @Cacheable 抛出错误为缓存操作返回空键
- angular - 如何在 ANGULAR 8 中双向绑定自定义指令?
- google-cloud-platform - 如何使用 Google API 获取 IAM 中所有成员的列表
- java - 如何从 Firebase 在 RecyclerView 中打开 URL?
- python - 如何将用户获取的文件路径保存在Json文件中?
- python - 帕菲属性错误
- android - 使用 IntDef 注释的正确方法是什么