很难找到这个问题的完全清晰的答案。在我的学习中,当我尝试将数据从request传递到models.py (当使用django管理器时)时,我被教导不要传递整个views.py对象。
但是,在我的当前场景中,我试图在models.py中设置验证方法(使用一个管理器),利用django-messages (https://docs.djangoproject.com/en/1.11/ref/contrib/messages/),在生成自定义错误时,需要request对象本身,例如:
messages.add_message(request, REG_ERR, 'First and last name are required must be at least 2 characters.', extra_tags="reg_errors")我试图使用管理器( models.py,https://docs.djangoproject.com/en/1.11/topics/db/managers/)将所有验证、错误消息生成以及创建或检索操作保存在https://docs.djangoproject.com/en/1.11/topics/db/managers/中,并将相关数据返回给我的views.py。
为了实现这一点,在我的views.py中,我创建了一个包含完整request对象本身的字典,我将其发送给所有验证/创建,然后在返回时检查是否存在错误,因为如果有任何验证错误被标记,则返回False。否则,成功页面将与新用户一起加载。
views.py
def register(request):
if request.method == "POST":
# Validate registration data submitted from registration form:
validated = User.objects.register_validate(request)
# If validation fails, load index page with request object
# (which `django messaging` has attached errors to):
if validated == False:
print "User could not be registered."
# Send back index with updated request object:
return render(request, "logreg/index.html")
# If validation successful, create new user and send it along with success page:
else:
# Load success page with `validated` user (already returned as a `dict` obj.)
return render(request, "logreg/success.html", validated)models.py
# Note: this function snippet is part of `class UserManager(models.Manager)`:
def register_validate(self, request):
# Check if first_name or last_name is less than 2 characters:
if len(request.POST["first_name"]) < 2 or len(request.POST["last_name"]) < 2:
# Add error to Django's error messaging:
messages.add_message(request, REG_ERR, 'First and last name are required must be at least 2 characters.', extra_tags="reg_errors")
# ... more validations ...
# Get current errors to check if any exist:
errors = get_messages(request)
# If no validation errors, hash password, create user and send new user back:
if len(errors) == 0:
# Hash Password:
hashed_pwd = bcrypt.hashpw(request.POST["password"].encode(), bcrypt.gensalt(14))
# Create new validated User:
validated_user = {
"logged_in_user": User(first_name=request.POST["first_name"], last_name=request.POST["last_name"], email=request.POST["email"], password=hashed_pwd)
}
# Save new User:
validated_user["logged_in_user"].save()
# Send newly created validated User back:
return validated_user
else:
return False问题:
以前,我已经通过request从request.POST["my_data"]对象中提取了所有数据(没有传递整个对象,而是将所需的内容提取到自定义dict中),并且是自定义生成错误消息。但是,我想练习使用django-messages (其中需要request对象作为参数,因为它将错误附加到其中)。因为我希望在models.py中进行所有验证,所以我发送了整个request对象(然后提取表单数据,但也用django-messages创建新消息)。
传递整个request 对象是否会导致性能严重下降,还是这种做法很糟糕?,我的学习经验太过偏激,以至于没有传递整个request对象,这让我有点困惑,特别是考虑到通过django-messages在models.py中生成错误,我需要访问完整的对象。
有什么想法吗?
注意:我的另一个想法是让我的验证函数返回一个错误列表,我可以迭代并生成django-messages错误(并在views.py中使用request对象),但是这会给控制器带来更多的逻辑/处理,并试图保持Manager (在models.py中)中的所有东西都是简洁的/组织的。
预先感谢您的阅读,如果我需要澄清这里的任何信息,请告诉我,presented...was试图提供尽可能少的代码块来提供上下文。
发布于 2017-05-19 02:51:02
附加整个request对象并不一定会影响性能,但它确实会引发安全问题,就好像整个request被发送到models.py一样,被欺骗的请求可能会干扰预期的函数。
通过与其他开发人员在一个空闲的通道上交谈,听起来大多数人都会在models.py中生成一个错误列表,这些错误将返回给views.py,在该列表中,他们调用django-messages模块并创建他们的消息。主要的参数是限制对request对象本身的访问,使其不受models.py的影响,并减少欺骗请求的风险。
https://stackoverflow.com/questions/44059835
复制相似问题