gpt4 book ai didi

python - django 属性错误 : object has no attribute 'get_bound_field'

转载 作者:太空狗 更新时间:2023-10-30 01:06:10 24 4
gpt4 key购买 nike

显然,我的模板上正在加载的表单需要一个未传递给它的字段。

谁能告诉我是什么导致了这个错误?

模板渲染出错

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>

这是由以下 View 提供的:

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}
),
}

模型.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

最佳答案

在您的表单中,您将获得一个 UserProfile 对象,然后将其指定为一个字段。但它不是一个字段,它是一个模型对象。

我不知道你想在那里做什么,但不要将该对象分配给字段字典。

关于python - django 属性错误 : object has no attribute 'get_bound_field' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39499798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com