gpt4 book ai didi

python - Django: 'AnonymousUser' 对象没有属性 'get_full_name'

转载 作者:太空宇宙 更新时间:2023-11-04 08:54:45 26 4
gpt4 key购买 nike

我正在尝试在 Django 中创建一个表单,其中包含一个表单字段,该表单字段具有从 User 对象中提取的默认值。

表单.py

class EvangelizedForm(forms.ModelForm):
#full_name = forms.CharField(help_text="Full Name")
email = forms.CharField(help_text="Email ID")
mobile_no = forms.CharField(help_text="Mobile number")
twitter_url = forms.CharField(help_text="Twitter URL")

模型.py

class Evangelized(models.Model):
full_name = models.CharField(max_length=128)
email = models.EmailField()
mobile_no = models.CharField(unique=True, max_length = 10, validators=[RegexValidator(regex='^\w{10}$', message='Mobile number should be strictly of 10 digits.')])
twitter_url = models.CharField(unique=True, max_length=128)

View .py

def fillform(request):
if request.method == 'POST':
form = EvangelizedForm(request.POST)
if form.is_valid():
form.save(commit=True)
return index(request)
else:
form.errors
else:
#form = EvangelizedForm()
form = EvangelizedForm(initial={'full_name': request.get_full_name()})

context = RequestContext(request,
{'request': request,
'user': request.user, 'form':form})

#return render(request, 'rango/fillform.html', {'form': form, 'context_instance':context})
return render_to_response('rango/fillform.html',
context_instance=context)

但是,我遇到了以下错误:

'AnonymousUser' object has no attribute 'get_full_name'

这个错误意味着什么,我该如何解决这个错误?

最佳答案

您很可能想要:

request.user.get_full_name() if request.user.is_authenticated() else ""

关于python - Django: 'AnonymousUser' 对象没有属性 'get_full_name',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31223780/

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