Hi Stack溢出的人:
我愿意接受以下新手python错误
中的一些方向我在forms.py中有两个模型表单:
class StrategicDnaValuePropositionForm(forms.ModelForm):
title = "Adn Estratégico y Propuesta de valor"
class Meta:
widgets = {
'familiar_enterprise':forms.RadioSelect,
'enterprise_concepts':forms.RadioSelect,
'value_purpose_target':forms.RadioSelect,
'competition_differentiation':forms.RadioSelect,
'goals_objectives':forms.RadioSelect,
'avoid_competition':forms.RadioSelect,
'enterprise_strategy':forms.RadioSelect,
}
model = StrategicDnaValueProposition
fields = ('familiar_enterprise', 'enterprise_size',
'enterprise_concepts', 'value_purpose_target', 'competition_differentiation', 'goals_objectives',
'avoid_competition')
class MarketingSalesForm(forms.ModelForm):
title = "Mercadeo y Ventas"
class Meta:
widgets = {
'key_activities':forms.RadioSelect,
'new_clients_strategies':forms.RadioSelect,
}
model = MarketingSales
fields = ('key_activities', 'new_clients_strategies',)
在我看来,我想生成这些模型表单的两个表单实例并发送到模板以保存它们。
我的views.py:
@login_required
def diagnostic_tool_questions(request):
user = request.user
# Populate the forms
form_diagnostic = []
if user.is_enterprise:
profile = user.get_enterprise_profile()
form_diagnostic.append({
'form_strategicdna': StrategicDnaValuePropositionForm,
'form_marketing_sales': MarketingSalesForm,
'instance': user.enterpriseprofile,
})
if request.method == 'POST':
# forms = [x['form'](data=request.POST,) for x in form_diagnostic]
forms = [x['form'](data=request.POST, instance=x['instance'],) for x in form_diagnostic]
if all([form.is_valid() for form in forms]):
for form in forms:
form.save()
return redirect('dashboard')
else:
forms = [x['form'](instance=x['instance']) for x in form_diagnostic]
return render(
request, 'enterprise_process/strategicdnavalueproposition_form.html',
{'forms':forms, 'userprofile':profile,})
我的模板是:
{% extends 'layout.html' %}
{% load bootstrap3 %}
{% block title_tag %}Herramienta de Diagnóstico | CRIMBLU {{ block.super }}{% endblock %}
{% block body_content %}
<div class="container">
<h1>Account Details</h1>
<form method="POST">
{% csrf_token %}
{% for form in forms %}
{% bootstrap_form form %}
{% endfor %}
<input type="submit" value="Save Changes" class="btn btn-default">
</form>
</div>
{% endblock %}
当我转到调用我的diagnostic_tool_questions视图的url时,我收到以下消息:
System check identified no issues (0 silenced).
May 26, 2017 - 19:40:20
Django version 1.11, using settings 'consultancy.settings.development'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Internal Server Error: /herramienta-diagnostico/preguntas/
Traceback (most recent call last):
File "/home/bgarcial/.virtualenvs/consultancy_dev/lib/python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/bgarcial/.virtualenvs/consultancy_dev/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/bgarcial/.virtualenvs/consultancy_dev/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/bgarcial/.virtualenvs/consultancy_dev/lib/python3.5/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/bgarcial/workspace/consultancy_project/enterprise_process/views.py", line 66, in diagnostic_tool_questions
forms = [x['form'](instance=x['instance']) for x in form_diagnostic]
File "/home/bgarcial/workspace/consultancy_project/enterprise_process/views.py", line 66, in <listcomp>
forms = [x['form'](instance=x['instance']) for x in form_diagnostic]
KeyError: 'form'
[26/May/2017 19:40:23] "GET /herramienta-diagnostico/preguntas/ HTTP/1.1" 500 89072
Django告诉我,我在行中有一些解释错误:
else:
forms = [x['form'](instance=x['instance']) for x in form_diagnostic]
KeyError: 'form'
我不知道出现的原因......
请原谅我的新手错误。