Django表单不会呈现

我的表单代码:

class PlanForm(forms.Form):
    a = forms.CharField(widget=forms.TextInput(attrs={"class", "add-up"}))
    b = forms.CharField(widget=forms.TextInput(attrs={"class", "add-up"}))

    class Media:
        javascript=('test.js', )

我的查看代码:

def plan_detail(request):
    if request.method == "POST":
        return render(request=request,
                  template_name='pasteur/plan_detail.html',
                      dictionary={ 'message': 'This was a post request. WTF!' })
    else:
        return render(request=request,
                  template_name='pasteur/plan_detail.html',
                      dictionary={ 'form': PlanForm() })

堆栈跟踪:

环境:

请求方法:GET请求URL:

Django版本:1.4 Python版本:2.7.2安装的应用程序:('django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.sites','django.contrib .messages','django.contrib.staticfiles','django.contrib.admin','django.contrib.admindocs','south','pasteur')已安装的中间件:('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware','django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware')

模板错误:在模板/Users/judithjacques/dana/dev/pasteur/src/pasteur/templates/pasteur/plan_detail.html中,第8行错误'set'object has no attribute'get'1:

Traceback:在get_response 111中的文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py”。response = callback(request,* callback_args, ** callback_kwargs)在plan_detail中的文件“/Users/judithjacques/dana/dev/pasteur/src/pasteur/views.py”13. dictionary = {'form':form})File“/Library/Frameworks/Python.framework/版本/ 2.7 / lib / python2.7 / site-packages / django / shortcuts / init .py“in render 44. return HttpResponse(loader.render_to_string(* args,** kwargs),File”/ Library / Frameworks / Python。 framework / Versions / 2.7 / lib / python2.7 / site-packages / django / template / loader.py“in render_to_string 176. return t.render(context_instance)File”/Library/Frameworks/Python.framework/Versions/2.7/ lib / python2.7 / site-packages / django / template / base.py“in render 140. return self._render(context)File”/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ site-packages / django / template / base.py“in _render 134. return self.nodelist.render(context)Fil e“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/base.py”in render 823. bit = self.render_node(node,context)File“/ Library / Frameworks / Python / Frameworks / Python.framework / Python.framework / Versions / 2.7 / lib / python2.7 / site-packages / django / template / debug.py“在render_node 74.返回node.render(context) framework / Versions / 2.7 / lib / python2.7 / site-packages / django / template / defaulttags.py“in render 281. return nodelist.render(context)File”/Library/Frameworks/Python.framework/Versions/2.7/ lib / python2.7 / site-packages / django / template / base.py“in render 823. bit = self.render_node(node,context)File”/Library/Frameworks/Python.framework/Versions/2.7/lib/python2 .7 / site-packages / django / template / debug.py“in render_node 74. return node.render(context)File”/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages /django/template/debug.py“in render 84. output = self.filter_expression.resolve(context)File”/ Library / Frameworks / Pyt 在解析571中使用“hon.framework / Versions / 2.7 / lib / python2.7 / site-packages / django / template / base.py”。obj = self.var.resolve(context)File“/Library/Frameworks/Python.framework /Versions/2.7/lib/python2.7/site-packages/django/template/base.py“解析为721.value = self._resolve_lookup(context)File”/Library/Frameworks/Python.framework/Versions/2.7/ lib / python2.7 / site-packages / django / template / base.py“in _resolve_lookup 772. current = current()File”/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site- packages / django / forms / forms.py“in as_ul 229. errors_on_separate_row = False)File”/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/forms.py “in _html_output 171. label = bf.label_tag(label)or''File”/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/forms.py“in label_tag 508. id_ = widget.attrs.get('id')或self.auto_id

异常类型:/ pasteur / plan / create / Exception中的AttributeError值:'set'对象没有属性'get'


正如错误所述, {"class", "add-up"}是一个集合。 你需要一本字典: {"class": "add-up"}

链接地址: http://www.djcxy.com/p/77261.html

上一篇: Django Form won't render

下一篇: required decorators on django class