如何在 Django 中自定义默认的 auth 登录表单?

你如何自定义Django中的默认登录表单?


# demo_project/urls.py

from django.contrib import admin

from django.urls import include, path


urlpatterns = [

    path('', include('pages.urls')),

    path('users/', include('users.urls')), # new

    path('users/', include('django.contrib.auth.urls')), # new

    path('admin/', admin.site.urls),

]


<!-- templates/registration/login.html -->

    <h2>Login</h2>

    <form method="post">

    {% csrf_token %}

    {{ form.as_p }}

    <button type="submit">Login</button>

    </form>

上面的代码有效,但我需要自定义 {{form.as_p}} 那个表单在哪里或者可以被覆盖?谢谢您的帮助。


守着一只汪
浏览 171回答 3
3回答

蛊毒传说

您可以覆盖默认的身份验证表单像这样from django.contrib.auth.forms import AuthenticationForm, UsernameFieldfrom django import formsclass UserLoginForm(AuthenticationForm):&nbsp; &nbsp; def __init__(self, *args, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; super(UserLoginForm, self).__init__(*args, **kwargs)&nbsp; &nbsp; username = UsernameField(widget=forms.TextInput(&nbsp; &nbsp; &nbsp; &nbsp; attrs={'class': 'form-control', 'placeholder': '', 'id': 'hello'}))&nbsp; &nbsp; password = forms.CharField(widget=forms.PasswordInput(&nbsp; &nbsp; &nbsp; &nbsp; attrs={&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'class': 'form-control',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'placeholder': '',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id': 'hi',&nbsp; &nbsp; &nbsp; &nbsp; }))在 url.py 中,您可以像这样传递此自定义身份验证表单from django.contrib.auth import viewsfrom myapp.forms import UserLoginFormurlpatterns = [&nbsp; &nbsp; path(&nbsp; &nbsp; &nbsp; &nbsp; 'login/',&nbsp; &nbsp; &nbsp; &nbsp; views.LoginView.as_view(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; template_name="login.html",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; authentication_form=UserLoginForm&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; name='login')]此外,您可以覆盖登录模板并根据您的要求自定义模板<form method="post">&nbsp; &nbsp; {{ form.non_field_errors }}&nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <label for="{{ form.username.id_for_label }}">Username:</label>&nbsp; &nbsp; &nbsp; &nbsp; {{ form.username }}&nbsp; &nbsp; &nbsp; &nbsp; {{ form.username.errors }}&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <label for="{{ form.password.id_for_label }}">Password:</label>&nbsp; &nbsp; &nbsp; &nbsp; {{ form.password }}&nbsp; &nbsp; &nbsp; &nbsp; {{ form.password.errors }}&nbsp; &nbsp; </div>&nbsp; &nbsp; <button type="submit">Login</button></form>

紫衣仙女

您可以通过覆盖/编辑templates/registration/login.html来自定义您的表单呈现,如下所示。例如,只有您可以根据需要更改 css 样式和格式。<h2>Login</h2><form method="post">&nbsp; &nbsp; {{ form.non_field_errors }}&nbsp; &nbsp; <div class="fieldWrapper">&nbsp; &nbsp; &nbsp; &nbsp; {{ form.username.errors }}&nbsp; &nbsp; &nbsp; &nbsp; <label for="{{ form.username.id_for_label }}">Username:</label>&nbsp; &nbsp; &nbsp; &nbsp; {{ form.username }}&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="fieldWrapper">&nbsp; &nbsp; &nbsp; &nbsp; {{ form.password.errors }}&nbsp; &nbsp; &nbsp; &nbsp; <label for="{{ form.password.id_for_label }}">Password:</label>&nbsp; &nbsp; &nbsp; &nbsp; {{ form.password }}&nbsp; &nbsp; </div>&nbsp; &nbsp; <button type="submit">Login</button></form>您也可以遍历表单的字段<h2>Login</h2><form method="post">&nbsp; &nbsp; {% for field in form %}&nbsp; &nbsp; &nbsp; &nbsp; <div class="fieldWrapper">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ field.errors }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{ field.label_tag }} {{ field }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% if field.help_text %}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p class="help">{{ field.help_text|safe }}</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {% endif %}&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; {% endfor %}&nbsp; &nbsp; <button type="submit">Login</button></form>

慕田峪4524236

从问题中,我假设您想更改 html 模板的外观而不是 python 表单类。在这种情况下,您需要做的就是包含具有匹配名称和 ids 属性的输入类型字段,这是默认的 django-auth 表单所期望的。您可以使用以下步骤实现此目的。在渲染时渲染模板(使用 {{form.as_p}})。检查元素并检查用户名、密码和提交按钮的名称和默认身份验证表单生成的 ID。使用您自己的自定义样式重新创建相同的标签。它类似于以下内容:<form method="POST">&nbsp; {% csrf_token %}&nbsp; <input type="input" class="form-control" name="username" id="inputEmail" placeholder="Username" required >&nbsp; <input type="password" class="form-control" name="password" id="inputPass" placeholder="Password" required>&nbsp; <button type="submit" style="opacity: 1 !important;">Login</button>&nbsp; <a href="/password_reset">Reset Password</a></form>在此之后,您可以发挥您的想象力并根据您的要求设计登录表单。希望这可以帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python