[英]Django Boolean Field used in view.py
I am trying to add a feature to where a new user needs to update his/her password on an initial login. 我正在尝试向新用户添加功能,以便新用户在初次登录时需要更新其密码。 I added a hidden BooleanField to my Profile model where default = True.
我在默认= True的Profile模型中添加了一个隐藏的BooleanField。
models.py models.py
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
force_password_change = models.BooleanField(default=True)
However, when trying to use force_password_change in my views.py, it never returns the correct value that I set in django's admin page. 但是,当尝试在我的views.py中使用force_password_change时,它永远不会返回我在django的管理页面中设置的正确值。
views.py views.py
if request.method == 'POST':
...
user = authenticate(request, username=username, password=password)
changepass = UserProfile.objects.get(user=request.user)
if user:
if changepass.force_password_change == True:
changepass.force_password_change = False
changepass.save()
return HttpResponseRedirect('/login/register/')
elif changepass.force_password_change == False:
if user.is_active:
login(request, user)
return HttpResponseRedirect('/main/')
else:
return HttpResponse("Your account has been disabled.")
It is currently giving me this error 目前正在给我这个错误
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\django\core\handlers\exception.py", line
41, in inner
response = get_response(request)
File "C:\Python34\lib\site-packages\django\core\handlers\base.py", line 187,
in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Python34\lib\site-packages\django\core\handlers\base.py", line 185,
in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "start\views.py", line 20, in user_login
changepass = UserProfile.objects.get(user=request.user)
File "C:\Python34\lib\site-packages\django\db\models\manager.py", line 85,
in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python34\lib\site-packages\django\db\models\query.py", line 380, in
get
self.model._meta.object_name
start.models.DoesNotExist: UserProfile matching query does not exist.
I also added AUTH_PROFILE_MODULE = 'start.UserProfile'
to my settings.py, so that does not seem to be the problem. 我还向我的settings.py添加了
AUTH_PROFILE_MODULE = 'start.UserProfile'
,所以这似乎不是问题。
You are forgetting to create UserProfile with the respected user 您忘记了与受尊敬的用户一起创建UserProfile
from django.db.models.signals import post_save
from models import UserProfile
from django.contrib.auth.models import User
def register(request):
if request.method == 'POST':
uf = UserForm(request.POST, prefix='user')
upf = UserProfileForm(request.POST, prefix='userprofile')
if uf.is_valid() and upf.is_valid():
user = uf.save()
userprofile = upf.save(commit=False)
userprofile.user = user
userprofile.save() # Are you missing this line ??
return django.http.HttpResponseRedirect(…something…)
This has nothing to do with boolean fields. 这与布尔字段无关。 The error is telling you that your specific User does not have a related entry in the UserProfile table.
该错误表明您的特定用户在UserProfile表中没有相关条目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.