显然,我的模板上加载的表单需要一个未传递给它的字段。
有谁能告诉我导致此错误的原因是什么?
模板渲染期间发生错误
In template C:\django\projectname\static\templates\link_list.html, error at line 114
'UserProfile' object has no attribute 'get_bound_field'
104
105 </p>
106
107 </form>
108 </div>
109 <div id="make-offer" style="display: none">
110
111 <p>Make an offer</p>
112 <form class=".formLayout" name="offer-form" action="" method="post" enctype="multipart/form-data">
113 {% csrf_token %}
114 {{ form.as_table }}
115 <input type="submit" class="data" style="font-family: VT323; font-size: 50%" value="Submit" />
116 </form>
117 </div>
118 </div>
119 </div>
120 </div>
以下视图提供此服务:
def link(request):
print("trying to authenticate...")
if request.user.is_authenticated():
user_profile = UserProfile.objects.get(user=request.user)
else:
return render(request, 'login.html')
if request.method == "POST":
print("request was a post.")
if request.is_ajax():
...
else:
if request.method == "POST":
form = OfferForm(request.POST, request.FILES, maker_id=user_profile.id)
if form.is_valid():
if request.POST.get('location'):
offer = form.save()
offer.save()
offer.maker = maker
offer.save()
else:
print form.errors
return render(request, "link_list.html", {'form' : form} )
else:
form = OfferForm(maker_id=user_profile.id)
print("request wasn't ajax.")
data = {'form': form}
return render(request, 'link_list.html', data)
以下是相关的模型和表格:
class OfferForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
maker_id = kwargs.pop('maker_id')
super(OfferForm, self).__init__(*args, **kwargs)
maker = UserProfile.objects.get(id=maker_id)
self.fields['maker'] = maker
class Meta:
model = Offer
fields = [
"name",
"value",
"description",
"tags",
"location",
"service",
"icon",
]
widgets = {
'name': forms.TextInput(
attrs={'id': 'name', 'class': '.formLayout', 'style': 'font-family: VT323; font-size: 60%', 'required': True, 'placeholder': 'name'}
),
'value': forms.TextInput(
attrs={'id': 'value', 'class': '.formLayout', 'style': 'font-family: VT323; font-size: 60%', 'required': True, 'placeholder': 'value'}
),
'description': forms.TextInput(
attrs={'id': 'description', 'class': '.formLayout', 'style': 'font-family: VT323; font-size: 60%', 'required': True, 'placeholder': 'description'}
),
'tags': forms.TextInput(
attrs={'id': 'tags', 'class': '.formLayout', 'style': 'font-family: VT323; font-size: 60%', 'required': True, 'placeholder': 'tags'}
),
'location': forms.TextInput(
attrs={'id': 'location', 'class': 'geo', 'data-geo': 'location', 'required': False, 'placeholder': 'leave blank to use your location'}
),
'service': forms.CheckboxInput(
attrs={'id': 'service', 'class': '.formLayout', 'required': False}
),
}
models.py
class UserProfile(models.Model):
user = models.OneToOneField(User)
website = models.URLField(blank=True, null=True)
location = models.CharField(max_length=200, null=True)
longitude = models.FloatField(null=True)
latitude = models.FloatField(null=True)
credit = models.FloatField(default=0, null=True)
picture = models.ImageField(upload_to=upload_location, blank=True)
def __unicode__(self):
return self.user.username
class Offer(models.Model):
name = models.CharField(max_length=120)
description = models.CharField(max_length=120)
value = models.FloatField(default=0.0)
tags = models.CharField(max_length=120, null=True)
maker = models.ForeignKey(UserProfile, null=True)
def __unicode__(self):
return self.name
答案 0 :(得分:5)
在您的表单中,您将获得一个UserProfile对象,然后将其指定为字段。但它不是一个领域,它是一个模型对象。
我不知道你在那里尝试做什么,但不要将该对象分配给字段dict。
答案 1 :(得分:0)
在models.py初始化函数中,应在如下字段之后使用.queryset:
def __init__(self, *args, **kwargs):
maker_id = kwargs.pop('maker_id')
super(OfferForm, self).__init__(*args, **kwargs)
maker = UserProfile.objects.get(id=maker_id)
self.fields['maker'].queryset = maker
答案 2 :(得分:0)
如果您尝试为该字段设置queryset
或(可用选项),则可以执行以下操作
def __init__(self, *args, **kwargs):
maker_id = kwargs.pop('maker_id')
super(OfferForm, self).__init__(*args, **kwargs)
maker = UserProfile.objects.get(id=maker_id)
self.fields['maker'].queryset = maker
在某些情况下,我想为该字段设置initial
或(默认)值。因此,我必须通过object
而不是queryset
def __init__(self, *args, **kwargs):
maker_id = kwargs.pop('maker_id')
super(OfferForm, self).__init__(*args, **kwargs)
maker = UserProfile.objects.get(id=maker_id)
self.fields['maker'].initial = maker.first()
您实际上可以做both
!设置queryset
和initial
值