向 django 密码重置表单添加样式

我想向 django 的默认密码重置表单添加样式,例如类和占位符


我的 urls.py 中有以下内容


from django.urls import path

from . import views

from django.contrib.auth import views as auth_views



urlpatterns = [


    # Password reset paths


    path('password_reset/', auth_views.PasswordResetView.as_view(template_name="main/password_reset.html"),name="reset_password"),

    path('password_reset_sent/', auth_views.PasswordResetDoneView.as_view(template_name="main/password_reset_sent.html"),name="password_reset_done"),

    path('reset_password/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(template_name="main/reset_password.html"),name="password_reset_confirm"),

    path('reset_password_complete/', auth_views.PasswordResetCompleteView.as_view(template_name="main/reset_password_complete.html"),name="password_reset_complete"),

]

在模板中


<form action="" method="POST">

            {% csrf_token %}

            {{form}}

            <input type="submit" name="Send email" class="btn btn-primary" >

        </form>


阿波罗的战车
浏览 109回答 3
3回答

呼唤远方

是的,当然你可以覆盖 django 表单。# forms.pyfrom django.contrib.auth.forms import PasswordResetFormclass UserPasswordResetForm(PasswordResetForm):&nbsp; &nbsp; def __init__(self, *args, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; super(UserPasswordResetForm, self).__init__(*args, **kwargs)&nbsp; &nbsp; email = forms.EmailField(label='', widget=forms.EmailInput(attrs={&nbsp; &nbsp; &nbsp; &nbsp; 'class': 'your class',&nbsp; &nbsp; &nbsp; &nbsp; 'placeholder': 'your placeholder',&nbsp; &nbsp; &nbsp; &nbsp; 'type': 'email',&nbsp; &nbsp; &nbsp; &nbsp; 'name': 'email'&nbsp; &nbsp; &nbsp; &nbsp; }))# urls.pyfrom .forms import UserPasswordResetFormpath('password_reset/', auth_views.PasswordResetView.as_view(&nbsp; &nbsp; template_name='main/password_reset.html',&nbsp; &nbsp; form_class=UserPasswordResetForm),name='password_reset'),

海绵宝宝撒

尝试使用django-crispy-forms。安装pip install django-crispy-forms将其添加到您的 INSTALLED_APPS 并选择样式:INSTALLED_APPS = ['crispy_forms',]CRISPY_TEMPLATE_PACK = 'bootstrap4'加载标签{% load crispy_forms_tags %}现在您可以设置表单的样式{{ form|crispy }}&nbsp; &nbsp;&nbsp;

慕田峪7331174

尝试使用 django-crispy-forms 和/或 django-widget-tweaks

www说

使用 Crispy Forms,这里有一篇关于它的详细解释的文章&nbsp;CrispyForms
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python