Django Form won't render
My form code:
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', )
My View Code:
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() })
Stack Trace:
environment:
Request Method: GET Request URL:
Django Version: 1.4 Python Version: 2.7.2 Installed Applications: ('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') Installed Middleware: ('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware')
Template error: In template /Users/judithjacques/dana/dev/pasteur/src/pasteur/templates/pasteur/plan_detail.html, error at line 8 'set' object has no attribute 'get' 1 :
Traceback: File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs) File "/Users/judithjacques/dana/dev/pasteur/src/pasteur/views.py" in plan_detail 13. dictionary={ 'form': form }) File "/Library/Frameworks/Python.framework/Versions/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.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/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 hon.framework/Versions/2.7/lib/python2.7/site-packages/django/template/base.py" in resolve 571. obj = self.var.resolve(context) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/base.py" in resolve 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') or self.auto_id
Exception Type: AttributeError at /pasteur/plan/create/ Exception Value: 'set' object has no attribute 'get'
As the error says, {"class", "add-up"}
is a set. You need a dictionary: {"class": "add-up"}
.
上一篇: Django ModelForm不保存数据到数据库
下一篇: Django表单不会呈现