Django admin 中的密码更改表单 URL 找不到具有正确 ID 的用户

Password change form URL in Django admin cannot find user with correct ID

提问人:kureal 提问时间:11/7/2023 更新时间:11/7/2023 访问量:18

问:

我的管理员设置遇到了一些问题。我正在使用自定义用户模型(称为 Org,用于上下文),除了密码更改表单外,其他一切都在管理员中完美运行。为了清楚起见,我说的是说

“原始密码不会存储,因此无法查看此用户的密码,但您可以使用此表单更改密码。”

但是,当我单击“此表单”时,我被重定向回管理员主页,并警告“ID 为'40/密码(40 是 ID)'的组织不存在。也许它被删除了?

内联和管理示例

# all inlines look like this:
class OrgContactInfoInline(admin.TabularInline):
    model = OrgContactInfo

@admin.register(Org)
class OrgAdmin(admin.ModelAdmin):
    inlines = [OrgContactInfoInline, OrgInfoInline, OrgLocationInline, ItemInline]
    add_form = CustomUserCreationForm
    form = CustomUserChangeForm
    model = Org
    ordering = ("-org_name",)```

重要表格

class CustomUserCreationForm(UserCreationForm):
    password = forms.CharField(
        widget=forms.PasswordInput(),
        label=mark_safe(
            "Your password must:<br />-Be longer than 8 characters<br />-Have an uppercase and lowercase characters<br />-Use a special character/number"
        ),
    )
    confirm_password = forms.CharField(widget=forms.PasswordInput())

    class Meta:
        model = Org
        fields = ["org_name"]


class CustomUserChangeForm(UserChangeForm):
    class Meta:
        model = Org
        fields = ["org_name"]

网址

urlpatterns = [
    #my urls
    path('', include('apps.homepage.urls')),
    path('accounts/', include('apps.accounts.urls')),
    path('mynonprofit/', include('apps.dashboard.urls')),

    #django urls
    path('accounts/', include('django.contrib.auth.urls')),
    path('admin/', admin.site.urls),
    re_path(r".*", TemplateView.as_view(template_name='index.html'))
]

最后,模型

class Org(AbstractUser):
    objects: CustomUserManager = CustomUserManager()

    org_name = models.CharField(
        unique=True,
        max_length=100,
        null=False,
        error_messages={"unique": "Orginization with this name already exists"},
    )
    username = None
    first_name = None
    last_name = None
    email = None
    USERNAME_FIELD = "org_name"
    REQUIRED_FIELDS = ()  # type: ignore

    def __str__(self) -> str:
        return self.org_name

    class Meta:
        verbose_name = "Org"
        verbose_name_plural = "Orgs"

除了玩弄我的 urls.py 文件之外,我几乎尝试了所有方法,我在写这篇文章时正在这样做。我已经更改了表单、管理员、模型等,但无济于事。

python django django-models django-admin

评论


答: 暂无答案