-1
我正在嘗試製作一個表單,可以將多個對象添加到列表中。 當我嘗試使用formset時出現錯誤,當我從模板中刪除formset時,所有內容似乎都能正常工作。formset應該包含有效的Django Formset
Parameter "formset" should contain a valid Django Formset.
我看了很多例子,但似乎沒有工作。
模板:
<form action="{% url 'usermanager:organization_edit_details' organization.id %}" method="post" class="form">
{% csrf_token %}
{% bootstrap_form form layout='inline' %}
{%bootstrap_formset formset %}
{% buttons %}
<button type="submit" class="btn btn-primary">
{% bootstrap_icon "star" %}Submit
</button><button class="btn btn-primary">Cancel -dead</button>
{% endbuttons %}
</form>
view.py
def organization_edit_details(request, organization_id):
organization = get_object_or_404(Organization, pk=organization_id)
form = OrganizationForm(request.POST or None, request.FILES or None, instance=organization)
formset = StaffFormSet(request.POST or None, instance = organization)
if form.is_valid():
organization.organization_name = form.cleaned_data['organization_name']
organization.description = form.cleaned_data['description']
organization.rules = form.cleaned_data['rules']
organization.open_times = form.cleaned_data['open_times']
organization.website = form.cleaned_data['website']
organization.email = form.cleaned_data['email']
organization.street_name = form.cleaned_data['street_name']
organization.street_number = form.cleaned_data['street_number']
organization.postcode = form.cleaned_data['postcode']
organization.country = form.cleaned_data['country']
organization.kvk_number = form.cleaned_data['kvk_number']
organization.save()
if formset.is_valid():
return HttpResponseRedirect(reverse('usermanager:organization_edit_details'))
return render(request, 'user_manager/organizationEdit.html',{'organization':organization, 'form':form})
forms.py
from django.forms import ModelForm, Textarea, inlineformset_factory
from organizations.models import Organization, StaffMemberList
class OrganizationForm(ModelForm):
class Meta:
model = Organization
fields = ['organization_name', 'description', 'rules','open_times',\
'website', 'email', 'street_name', 'street_number', 'postcode', 'country']
widgets = {
'description': Textarea(attrs={'cols': 40, 'rows': 20}),
'rules': Textarea(attrs={'cols': 40, 'rows': 15}),
'open_times': Textarea(attrs={'cols': 40, 'rows': 7})
}
class StaffForm(ModelForm):
class Meta:
model = StaffMemberList
fields = ['username',]
StaffFormSet = inlineformset_factory(Organization, StaffMemberList, fields=('username',))
Trackeback:
Environment:
Request Method: GET
Request URL: http://localhost:8000/user/organizationmanager/1/edit/
Django Version: 1.10.4
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'organizations.apps.OrganizationsConfig',
'news.apps.NewsConfig',
'events.apps.EventsConfig',
'user_manager.apps.UserManagerConfig',
'bootstrap3',
'django_forms_bootstrap',
'registration',
'debug_toolbar']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware']
Template error:
In template /home/dave/Projecten/Web/TotalMTB/user_manager/templates/user_manager/organizationEdit.html, error at line 19
Parameter "formset" should contain a valid Django Formset. 9 : {% block content %}
10 : <div class="row">
11 : <div class="col-xs-6 organization-edit">
12 : <h3>You are editing the page of: <b>{{ organization.organization_name }}</b></h3>
13 :
14 : {%if error_message %}<p><strong>{% bootstrap_form_errors form layout='inline' %}</strong></p>{% endif %}
15 :
16 : <form action="{% url 'usermanager:organization_edit_details' organization.id %}" method="post" class="form">
17 : {% csrf_token %}
18 : {% bootstrap_form form layout='inline' %}
19 : {%bootstrap_formset formset %}
20 : {% buttons %}
21 : <button type="submit" class="btn btn-primary">
22 : {% bootstrap_icon "star" %}Submit
23 : </button><button class="btn btn-primary">Cancel -dead</button>
24 : {% endbuttons %}
25 : </form>
26 : </div>
27 : </div>
28 : {% endblock %}
29 :
Traceback:
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/home/dave/Projecten/Web/TotalMTB/user_manager/views.py" in organization_edit_details
43. return render(request, 'user_manager/organizationEdit.html',{'organization':organization, 'form':form})
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/shortcuts.py" in render
30. content = loader.render_to_string(template_name, context, request, using=using)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/template/loader.py" in render_to_string
68. return template.render(context, request)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/template/backends/django.py" in render
66. return self.template.render(context)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/template/base.py" in render
208. return self._render(context)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/test/utils.py" in instrumented_test_render
94. return self.nodelist.render(context)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/template/base.py" in render
994. bit = node.render_annotated(context)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/template/base.py" in render_annotated
961. return self.render(context)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/template/loader_tags.py" in render
174. return compiled_parent._render(context)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/test/utils.py" in instrumented_test_render
94. return self.nodelist.render(context)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/template/base.py" in render
994. bit = node.render_annotated(context)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/template/base.py" in render_annotated
961. return self.render(context)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/template/loader_tags.py" in render
70. result = block.nodelist.render(context)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/template/base.py" in render
994. bit = node.render_annotated(context)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/template/base.py" in render_annotated
961. return self.render(context)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/template/loader_tags.py" in render
70. result = block.nodelist.render(context)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/template/base.py" in render
994. bit = node.render_annotated(context)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/template/base.py" in render_annotated
961. return self.render(context)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/django/template/library.py" in render
203. output = self.func(*resolved_args, **resolved_kwargs)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/bootstrap3/templatetags/bootstrap3.py" in bootstrap_formset
268. return render_formset(*args, **kwargs)
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/bootstrap3/forms.py" in render_formset
30. return renderer_cls(formset, **kwargs).render()
File "/home/dave/Projecten/Web/Django/venv/lib/python3.5/site-packages/bootstrap3/renderers.py" in __init__
83. 'Parameter "formset" should contain a valid Django Formset.')
Exception Type: BootstrapError at /user/organizationmanager/1/edit/
Exception Value: Parameter "formset" should contain a valid Django Formset.
{%bootstrap_formset formset%} to {%bootstrap_formset formset%} –
我認爲'formset = StaffFormSet(request.POST or None,instance = organization)'實例給staff模型。但不是組織.. –