扩展 Django 管理员登录(用户名、密码、站点)

发布于 2024-09-15 16:54:10 字数 256 浏览 4 评论 0原文

我正在尝试扩展 Django 管理员登录。大多数资源都指向登录后扩展视图。

我想将网站添加到登录条件中。

因此,而不是

  • 用户名
  • 密码

它将是

  • 用户名
  • 密码
  • 站点

这样站点将检查用户是否以管理员身份属于该站点,如果是,它将仅加载属于该站点的数据。

谢谢

干杯, 米奇

I'm trying to extend Django Admin Login. Most of the resources pointed towards extending views after the Login.

I wanted to add sites to the Login criteria.

So instead of

  • Username
  • Password

It will be

  • Username
  • Password
  • Site

Such that the Site will check whether the user belongs to the Site as admin and if it is, it will load only data belongs to the site.

Thanks

Cheers,
Mickey

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

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

发布评论

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

评论(2

梅窗月明清似水 2024-09-22 16:54:10

我不确定,因为我是 django 的新手。

我将从项目文件夹中的原始 django 代码中复制管理代码。然后我会按照您想要的方式更改它并将其放入已安装的应用程序中。

我希望,我能帮助你。正如我所说,我是 django 的新手。

克拉猎手

I am not sure because I am newbee in django.

I would copy the admin code from the original django code in my profject folder. Then I would change it like you want it and put it in installed apps.

I hope, I could help you. As I said, I am a newbee in django.

Craphunter

甜是你 2024-09-22 16:54:10

为此,您使用用户配置文件

这是一个基本示例(此代码将位于您应用的 models.py 中):

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

# Define a signal handler to be called after saving
# a User object
def user_save_handler(sender, **kwargs):
    # If the save was creating a new user as opposed
    # to updating an existing one, then create a 
    # UserProfile object and associate it with the User
    if kwargs['created']:
        # kwargs['instance'] is the User object we just
        # created
        UserProfile(user=kwargs['instance']).save()

# Hook the signal handler up to the User model's post_save
# signal
post_save.connect(user_save_handler, sender=User)

# Define your UserProfile class as usual. 
class UserProfile(models.Model):
    # user is a one-to-one reference to the associated
    # User object. You need this field
    user = models.OneToOneField(User)

    # Now define any other fields you care about
    birthday = models.DateField()

You use user profiles for this.

Here's a basic example (this code would go in your app's models.py):

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

# Define a signal handler to be called after saving
# a User object
def user_save_handler(sender, **kwargs):
    # If the save was creating a new user as opposed
    # to updating an existing one, then create a 
    # UserProfile object and associate it with the User
    if kwargs['created']:
        # kwargs['instance'] is the User object we just
        # created
        UserProfile(user=kwargs['instance']).save()

# Hook the signal handler up to the User model's post_save
# signal
post_save.connect(user_save_handler, sender=User)

# Define your UserProfile class as usual. 
class UserProfile(models.Model):
    # user is a one-to-one reference to the associated
    # User object. You need this field
    user = models.OneToOneField(User)

    # Now define any other fields you care about
    birthday = models.DateField()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文