Django-如何以最佳方式编写用户和配置文件处理? [英] Django - how to write users and profiles handling in best way?

查看:143
本文介绍了Django-如何以最佳方式编写用户和配置文件处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的网站,要求处理用户和个人资料.最初的最初想法是在用户处理中使用django的构建,但是随后用户模型过于狭窄并且不包含我需要的字段.文档中提到了用户个人资料,但是djangobook中的用户个人资料"部分已从django 1.0中删除(理想情况下,该解决方案应与django 1.2一起使用),并且互联网上充斥着各种解决方案,这使得选择不那么容易(例如,用户模型继承,用户个人资料和Django信号等).

I am writing simple site that requires users and profiles to be handled. The first initial thought is to use django's build in user handling, but then the user model is too narrow and does not contain fields that I need. The documentation mentions user profiles, but user profiles section has been removed from djangobook covering django 1.0 (ideally, the solution should work with django 1.2), and the Internet is full of different solutions, not making the choice easier (like user model inheritance, user profiles and django signals, and so on).

我想知道,如何以良好,现代,快速和安全的方式编写此内容.我应该尝试扩展django内置用户模型,还是应该创建足够宽的用户模型以保留所需的所有信息?在下面,您可能会从工作解决方案中找到一些规格和期望值:

I would like to know, how to write this in good, modern, fast and secure way. Should I try to extend django builtin user model, or maybe should I create my own user model wide enough to keep all the information I need? Below you may find some specifications and expectations from the working solution:

  • 用户应该能够注册和认证
  • 每个用户都应该拥有个人资料(或具有所有必填字段的模型)
  • 用户不需要django内置的管理面板,但他们需要通过简单的Web表单来编辑其个人资料/模型

请让我知道您如何解决应用程序中的这些问题,以及使用django处理用户的最佳最新方法是什么.任何与文章/博客或代码示例的链接都将受到高度赞赏!

Please, let me know how do you solve those issues in your applications, and what is the best current way to handle users with django. Any links to articles/blogs or code examples are highly appreciated!

推荐答案

用户应该能够注册和认证

users should be able to register and authenticate

django.contrib.auth是所需的模块.确保检查文档中的自定义登录表格.

django.contrib.auth is the module you want. Be sure to check the docs for custom login forms.

每个用户都应该拥有个人资料(或带有所有必填字段的模型)

every user should have profile (or model with all required fields)

如其他人所述,您需要设置settings.AUTH_PROFILE_MODULE.

You need to set settings.AUTH_PROFILE_MODULE, as noted by others.

有关设置用户个人资料模型的信息,可用于最新版本

Information about setting up the user profile model is available for the latest version, 1.1, and 1.0. It hasn't been dropped.

用户不需要django内置的管理面板,但他们需要通过简单的Web表单编辑其个人资料/模型

users dont need django builtin admin panel, but they need to edit their profiles/models via simple web form

您可以像创建其他任何应用程序一样创建表单并进行查看;也许制作一个用户控制面板"应用来处理这些事情.然后,您的视图将与django.contrib.auth.models.Userdjango.contrib.auth.models.Group模型进行交互.您可以将其设置为执行所需的任何操作.

You can create a form and view just like you would for any other app; maybe make a "user control panel" app for handling these things. Your views would then interact with the django.contrib.auth.models.User and django.contrib.auth.models.Group models. You can set this up to do whatever you need.

以回答形式回答您的问题(分页查看Alex Trebek)...

Responding to your questions-in-the-form-of-an-answer (paging Alex Trebek)...

第二版djangobook,涵盖了django 1.0(比0.96更接近1.2)不再具有该信息,这让我感到非常困惑-有什么变化吗?还有其他更好,更安全的方式来处理用户及其个人资料吗?因此,这个问题问了.

The second version of djangobook, covering django 1.0 (that is way closer to 1.2 than 0.96) no longer has that information anywhere, what makes me highly confused - has anything changed? Is there other, better, more secure way to handle users and their profiles? Therefore this question asked.

我不推荐djangobook作为参考;在这个话题上已经过时了.用户配置文件存在,我在Django 1.1.1站点中使用它们.我什至从NIS中填充它们.

I wouldn't recommend djangobook as a reference; it's out of date on this topic. User profiles exist and I'm using them in my Django 1.1.1 site; I'm even populating them from NIS.

请使用我上面提供的链接.他们直接访问实际Django文档,并且具有权威性.

Please use the links I provided above. They go directly to the actual Django documentation and are authoritative.

顺便说一句,我忘了问,大家引用的方式(即AUTH_PROFILE_MODULE)在注册后是否会自动创建

By the way, I forgot to ask, if the way you all refer to (that is AUTH_PROFILE_MODULE) will create automatically upon registration

已在文档中回答.

并要求配置文件在任何操作下都存在(用户不存在,已填充的配置文件不应该存在,这就是为什么我在考虑以某种方式扩展用户模型)

and require the profile to exist upon any action (user withoud existing, filled profile should not exists, this is why I was thinking about extending User model somehow)?

如果调用User.get_profile(),则配置文件必须存在.

The profile needs to exist if User.get_profile() is called.

它也会更新吗(人们在与此主题相关的各种博客中都提到了信号")?

Will it get updated as well (people are mentioning 'signals' on various blogs related to this subject)?

它与其他任何模型一样:仅在更改字段并调用save()时才会更新.

It's like any other model: it only gets updated when you change the fields and call save().

信号部分是您如何钩挂要创建的函数新用户的个人资料:

The signal part is how you hook in a function to create a profile for a new User:

from django.db.models.signals import post_save
from django.contrib.auth import User
from myUserProfileApp import UserProfile

def make_user_profile(sender, **kwargs):
    if 'created' not in kwargs or not kwargs['created']:
        return

    # Assumes that the `ForeignKey(User)` field in "UserProfile" is named "user".
    profile = UserProfile(user=kwargs["instance"])
    # Set anything else you need to in the profile, then...
    profile.save()

post_save.connect(make_user_profile, sender=User, weak=False)

这只会为新用户创建一个新的配置文件.现有用户需要手动添加配置文件:

This only creates a new profile for a new User. Existing Users need to have profiles manually added:

$ ./manage.py shell
>>> from django.contrib.auth import User
>>> from myUserProfileApp import UserProfile
>>> for u in User.objects.all():
...  UserProfile(user=u).save() # Add other params as needed.
...

如果您有一些拥有个人资料的用户,而有些没有个人资料,则需要做更多的工作:

If you have some users with profiles and some without, you'll need to do a bit more work:

>>> for u in User.objects.all():
...  try:
...   UserProfile(user=u).save() # Add other params as needed.
...  except:
...   pass

这篇关于Django-如何以最佳方式编写用户和配置文件处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆