python - view.py 中使用的 Django bool 字段

标签 python django

我正在尝试添加一项功能,新用户需要在首次登录时更新他/她的密码。我在我的 Profile 模型中添加了一个隐藏的 BooleanField,其中默认值 = True。

模型.py

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    force_password_change = models.BooleanField(default=True)

但是,当尝试在我的views.py中使用force_password_change时,它永远不会返回我在django管理页面中设置的正确值。

View .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.")

目前给我这个错误

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.

我还在我的settings.py中添加了AUTH_PROFILE_MODULE = 'start.UserProfile',所以这似乎不是问题。

最佳答案

您忘记与受尊敬的用户创建 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…)

关于python - view.py 中使用的 Django bool 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45042544/

相关文章:

python - Django 时区不知道 DateTimeField 错误

python - 如何使用基于类的装饰器的缩写符号向 __call__ 提供 *args 和 **kwargs?

IIS 上的 Django : Debugging IIS Error due to FastCGI request timeout on large file upload

python - S3 的 Django 存储后端

python - 在 Ajax 调用之后使用 Django 模板呈现 JSON 对象

django - 不能分配必须是实例 Django

python - py.test - 在不同目录中收集 2 个 conftest.py 时出错

python - pip3 列表出现 AssertionError

Python,在 excel 工作表中搜索列中的特定字符串并将这些行提取到文本文件

python - 如何在 DRF 中使字段可编辑=False