【问题标题】:django UserChangeForm not rendering "this form" link correctly with form.as_pdjango UserChangeForm 未正确呈现“此表单”与 form.as_p 的链接
【发布时间】:2022-01-12 15:30:48
【问题描述】:

我正在尝试使用 form.as_p 来渲染它,并且它主要是按照它应该的方式渲染。但是,更改密码部分的链接未正确呈现。我得到的不是“此表单”上的蓝色链接...

“未存储原始密码,因此无法查看此用户的密码,但您可以使用此表单更改密码。” 有人处理过这个问题吗?

这是我的 HTML:

{% extends 'base.html' %}
{% block title %}Edit Profile{% endblock %}
{% block content %}

<h1>Edit Profile...</h1>
<br/>
<div class="form-group">
    <form method="POST">
        {% csrf_token %}
        {{ form.as_p }}
        <button class="btn btn-secondary">Update Profile</button>
    </form>
</div>


{% endblock %}

还有我的forms.py:

from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.models import User 
from django import forms

class EditProfileForm(UserChangeForm):
    email = forms.EmailField(widget=forms.EmailInput(attrs={'class': 'form-control'}))
    first_name = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'class': 'form-control'}))
    last_name = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'class': 'form-control'}))
    username = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'class': 'form-control'}))
    last_login = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'class': 'form-control'}))
    is_superuser = forms.CharField(max_length=100, widget=forms.CheckboxInput(attrs={'class': 'form-check'}))
    is_staff = forms.CharField(max_length=100, widget=forms.CheckboxInput(attrs={'class': 'form-check'}))
    is_active = forms.CharField(max_length=100, widget=forms.CheckboxInput(attrs={'class': 'form-check'}))
    date_joined = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'class': 'form-control'}))

    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', 'password', 'last_login', 'is_superuser', 'is_staff', 'is_active', 'date_joined')

Views.py:

from django.shortcuts import render
from django.views import generic
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.urls import reverse_lazy
from .forms import SignUpForm, EditProfileForm

from members.forms import SignUpForm

class UserRegisterView(generic.CreateView):
    form_class = SignUpForm
    template_name = 'registration/register.html'
    success_url = reverse_lazy('login')

class UserEditView(generic.UpdateView):
    form_class = EditProfileForm
    template_name = 'registration/edit_profile.html'
    success_url = reverse_lazy('home')

    def get_object(self):
        return self.request.user

urls.py:

from django.urls import path
from .views import UserRegisterView, UserEditView
from django.contrib.auth import views as auth_views

urlpatterns = [
    path('register/', UserRegisterView.as_view(), name="register"),
    path('edit_profile/', UserEditView.as_view(), name="edit_profile"),
    path('password/', auth_views.PasswordChangeView.as_view())

一切都正确呈现,除了我在密码部分的页面上得到这个:

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

有人知道解决这个问题的方法吗?

【问题讨论】:

  • 您不应允许用户从您网站的“更改个人资料”部分更改密码。对于更改密码,Django 已经提供了一个可以与 CreateView 一起使用的表单。 django.contrib.auth.forms.PasswordChangeForm
  • 对,我明白。我只是想弄清楚为什么 标记不能正确呈现。稍后我将添加 PasswordChangeForm

标签: python django django-forms passwords render


【解决方案1】:

在最新版本的 Django 中,默认情况下帮助文本安全UserChangeForm 未更新以反映此更改。

为了获得所需的输出,您必须在UserChangeForm 中用mark_safe 覆盖password

from django.contrib.auth.forms import ReadOnlyPasswordHashField, UserChangeForm
from django.utils.safestring import mark_safe

class UserEditView(UserChangeForm):
    password = ReadOnlyPasswordHashField(
        label=("Password"),
        help_text=mark_safe(
            'Raw passwords are not stored, so there is no way to see this '
            'user’s password, but you can change the password using '
            '<a href="{}">this form</a>.'
        ),
    )
    
    ...

【讨论】:

    猜你喜欢
    • 2019-01-21
    • 2023-04-09
    • 2018-02-14
    • 2020-09-06
    • 2014-01-31
    • 2012-03-05
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多