首页 > 解决方案 > “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/'

标签: pythondjangodjango-formsdjango-templates

解决方案


推荐阅读