Django - 如何以最佳方式编写用户和配置文件处理?

发布于 2024-08-28 19:26:24 字数 543 浏览 10 评论 0原文

我正在编写一个简单的网站,需要处理用户和配置文件。第一个最初的想法是在用户处理中使用 django 的构建,但是用户模型太窄并且不包含我需要的字段。文档提到了用户配置文件,但用户配置文件部分已从涵盖 django 1.0 的 djangobook 中删除(理想情况下,该解决方案应适用于 django 1.2),并且互联网上充满了不同的解决方案,并没有使选择变得更容易(例如用户模型继承,用户配置文件和 django 信号等)。

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

  • 用户应该能够注册和身份验证
  • 每个用户应该有配置文件(或具有所有必填字段的模型)
  • 用户不需要 django 内置管理面板,但他们需要编辑他们的配置文件/通过简单的 Web 表单进行模型

请让我知道您如何解决应用程序中的这些问题,以及当前使用 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).

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:

  • users should be able to register and authenticate
  • every user should have profile (or model with all required fields)
  • users dont need django builtin admin panel, but they need to edit their profiles/models via simple web form

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

少跟Wǒ拽 2024-09-04 19:26:24

用户应该能够注册和身份验证

django.contrib.auth 是您想要的模块。请务必检查文档以了解 自定义登录表格。

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

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

有关设置用户配置文件模型的信息,请参阅 最新版本1.11.0 。它还没有被丢弃。

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

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

编辑:以答案的形式回答您的问题(寻呼亚历克斯·特雷贝克)...

djangobook 的第二个版本,涵盖 django 1.0(比 0.96 更接近 1.2),在任何地方都不再有该信息,是什么让我非常困惑 - 有什么变化吗?是否有其他更好、更安全的方式来处理用户及其个人资料?因此提出了这个问题。

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

请使用我上面提供的链接。它们直接进入实际的 Django 文档并且具有权威性。

对了,忘了问,你们提到的方式(即AUTH_PROFILE_MODULE)注册时会自动创建吗

在文档中回答。

并要求配置文件在任何操作中都存在(没有现有的、填充的配置文件的用户不应该存在,这就是我考虑以某种方式扩展用户模型的原因)?

如果调用 User.get_profile(),则配置文件需要存在。

它也会更新(人们在与此主题相关的各种博客上提到“信号”)吗?

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

signal 部分是如何挂钩函数来创建配置文件的新用户:

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)

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

$ ./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.
...

如果您有一些用户有配置文件,而另一些则没有,则需要做更多的工作:

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

users should be able to register and authenticate

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)

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.

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

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.

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

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.

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.

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

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

Answered in the docs.

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)?

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)?

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
心奴独伤 2024-09-04 19:26:24

在我看来,当前版本的 Django 文档和 Django 书籍都有这方面的章节。

Seems to me like the current version of the Django docs and the Django book both have sections for this.

半衬遮猫 2024-09-04 19:26:24

Django 支持 UserProfile 模型(您自己创建的)开箱即用。您可以在 settings.py 文件中使用以下命令进行分配:AUTH_PROFILE_MODULE。话虽如此,我同意你的观点,一开始有点令人困惑。

我的处理方式是使用我想要的字段创建我自己的 UserProfile 模型,并通过设置(上面)将其挂接到 Django User 模型中。我相信这是首选方式,并且可能比扩展基本用户模型更好。

您可以通过 User.get_profile() 访问您的个人资料。

github 上有几个面向 UserProfile 的项目。如果您想要一些代码示例,您可以在那里查看。

Django supports a UserProfile model (of your own creation) right out of the box. You can assign this in your settings.py file with: AUTH_PROFILE_MODULE. That being said, I will agree with you that it is a little confusing at first.

How I handled it was to create my own UserProfile model with the fields I wanted and hook it into the Django User model via the settings (above). I believe this is the preferred way and probably better than extending the base User model.

You can access your profile through User.get_profile().

There are a couple projects on github that are UserProfile oriented. If you wanted some code examples, you could look there.

友欢 2024-09-04 19:26:24

谢谢大家的回答!我知道 django 开发文档提到了用户配置文件,但它非常简短(大约几行)并链接到包含有关用户配置文件信息的 djangobook,但是......它的第一个版本,涵盖 django 0.96。 djangobook 的第二个版本,涵盖 django 1.0(比 0.96 更接近 1.2),在任何地方都不再有该信息,是什么让我非常困惑 - 有什么变化吗?是否有其他更好、更安全的方式来处理用户及其个人资料?所以才问出这个问题。

Thank you all for your answers! I know that django dev documentation mentions user profiles, but does it very briefly (roughly few lines) and links to djangobook containing information about user profiles, but... to its first version, covering django 0.96. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文