我收到错误"输入对象'输入'没有属性' company',但是在model.py中确实存在,所以必定还有其他一些我没有看到的错误。非常感谢您的任何帮助。
input- models.py
class Input(models.Model):
company=models.CharField(max_length=100,default='Empty')
region=models.CharField(max_length=100)
start_date=models.DateField(auto_now=False, auto_now_add=False)
def __unicode__(self):
return self.company
forms.py
class InputForm(forms.ModelForm):
company=forms.CharField(required=True)
regionlist = forms.ModelChoiceField(queryset=Dupont.objects.values('region').distinct())
start_date=forms.DateField(widget=DateInput(),required=True)
class Meta:
model = Input
fields = ('company', 'region','start_date')
widgets = {
'start_date': forms.DateInput(attrs={'class':'datepicker'}),
'end_date': forms.DateInput(attrs={'class':'datepicker'}),
}
views.py
def input(request):
if request.method == 'POST':
form = InputForm(request.POST or None, request.FILES or None)
if form.is_valid():
print 'is valid'
company = form.cleaned_data['company']
region = form.cleaned_data['region']
start_date= form.cleaned_data['start_date']
form.save()
return redirect('result')
else:
return render_to_response('input.html',{'form': form},context_instance=RequestContext(request))
else:
form = InputForm(initial={'company':'coco','uom':'M$'},instance=Input)
return render_to_response('input.html',{'form': form})
HTML
<form method="post" action="{% url 'result' %}">
{% csrf_token %}
<!--enter company name-->
<div class="field">
<p>Company:<input type="text" name="company" value="{{company}}"/>
</div>
<!--select region from drop down list-->
<div class="field" >
<label> Select the Region:
{{ form.regionlist }}
{% for region in form.regionlist.choices %}
<option value="{{ val }}" name= "region" {% ifequal data.val val %}selected {% endifequal %}></option>
{% endfor %}
</label>
</div>
<!--select start date from drop down list-->
<label for="startDate">Start Month:</label>
<input name="start_date" id="start_date" class="date-picker"/>
<!--submit-->
<div class="fieldWrapper">
<p><input type="submit" value="Submit" /></p></div>
</form>
引用
Traceback:
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\views\decorators\csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "C:\Users\user\Desktop\SCOR\inputform\views.py" in input
34. form = InputForm(initial={'company':'coco','uom':'M$'},instance=Input)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\forms\models.py" in __init__
320. object_data = model_to_dict(instance, opts.fields, opts.exclude)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\forms\models.py" in model_to_dict
153. data[f.name] = f.value_from_object(instance)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\db\models\fields\__init__.py" in value_from_object
918. return getattr(obj, self.attname)
Exception Type: AttributeError at /input
Exception Value: type object 'Input' has no attribute 'company'
答案 0 :(得分:2)
在这一行:
if panel width / panel height > 4 / 3
// too wide
// use panel height as image height
// calculate image width from image height
else
// use panel width as image width
// calculate image height from image width
您将form = InputForm(initial={'company':'coco','uom':'M$'},instance=Input)
类作为Input
传递,而不是instance
实例。要传递一个实例,您需要调用该类,即:
Input
但在这里完全没用,因为如果您不提供实例,form = InputForm(initial={'company':'coco','uom':'M$'},instance=Input())
已经知道要实例化哪个模型类。