[英]AttributeError: 'bool' object has no attribute
我正在研究Django項目,從本質上講,我編寫了一個存儲用戶詳細信息的模型。 他們在注冊后完成了此配置文件,並且我在用戶模型中有一個布爾值,該值指示他們是否已完成此自定義配置文件,因此我可以在模板中進行更改。
當提交第二個配置文件頁面的表單時,我希望將Bool從False更新為True,但出現錯誤:
'bool' object has no attribute 'has_created_artist_profile'
參見下面的代碼:
views.py
def ArtistEditView(request):
artist = Artist.objects.get(user=request.user)
current_artist = request.user
artist_status = current_artist.has_created_artist_profile
if request.method == 'POST':
form = ArtistForm(request.POST, request.FILES, instance=artist)
if form.is_valid():
artist_status.has_created_artist_profile = True
artist_status.save()
form.save()
return redirect(reverse('artist_home'))
else:
artist_dict = model_to_dict(artist)
form = ArtistForm(artist_dict)
return render(request, 'artist/artist_edit.html', {'form': form})
forms.py
class ArtistForm(forms.ModelForm):
class Meta:
model = Artist
exclude =['user', ]
任何人都可以提出一種更好的方法來更新此錯誤/擺脫錯誤嗎?
'bool' object has no attribute 'has_created_artist_profile'
意味着您正在嘗試訪問布爾對象( True
或False
)而不是對象的has_created_artist_profile
屬性。
例如: True.has_created_artist_profile
將產生完全相同的錯誤。
從代碼中,將artist_status
設置為對象的一部分布爾值( current_artist
),然后嘗試從該布爾值訪問屬性。
根據建議,您已經刪除了artist_status
變量,並且現在直接使用current_artist
對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.