简体   繁体   English

为 django-allauth 覆盖 django-crispy-forms 形式的电子邮件标题

[英]Override e-mail title in form django-crispy-forms for django-allauth

I can not find a way to redefine the title and placeholder for the e-mail field in the form of a django-allauth created using django-crispy-forms.我找不到以使用 django-crispy-forms 创建的 django-allauth 的形式重新定义电子邮件字段的标题和占位符的方法。

templates\account\login.html

{% extends '_base.html' %}
{% load crispy_forms_tags %}


{% block title %}Log In{% endblock title %}

{% block content %}
<h2>Log In</h2>

<form method="post">
    {% csrf_token %}
    {{ form|crispy }}
    <button class="btn btn-success" type="submit">Log In</button>
</form>
{% endblock content %}

this code form example created login page with email and password此代码表单示例使用 email 和密码创建了登录页面

urls.py project level

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    # Django admin
    path('admin/', admin.site.urls),

    # User management
    path('accounts/', include('allauth.urls')),
]

settings设置

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',
)
ACCOUNT_AUTHENTICATION_METHOD = 'email'

in forms to me i found a way to override the password field label在 forms 对我来说,我找到了一种方法来覆盖密码字段 label

app\forms.py
from allauth.account.forms import LoginForm, PasswordField

class YourLoginForm(LoginForm):
    password = PasswordField(label="custom_label_for_pass ")

I want to know how I can flexibly manage forms that are created in this way.我想知道如何灵活管理以这种方式创建的forms。 Thanks in advance for your help.在此先感谢您的帮助。

Have you read Log in / Sign up directly on home page ?您是否阅读过在主页上直接登录/注册

The solution there will work, but is maybe not the right or easiest way.那里的解决方案可行,但可能不是正确或最简单的方法。 Probably you are past this but hope it helps someone.可能你已经过去了,但希望它可以帮助某人。

You can customize the login form in django-allauth with something like this:你可以在 django-allauth 中自定义登录表单,如下所示:

in app\forms.py:在 app\forms.py 中:

from allauth.account.forms import LoginForm

class CustomLoginForm(LoginForm):
def __init__(self, *args, **kwargs):
    super(CustomLoginForm, self).__init__(*args, **kwargs)

    # email field
    self.fields["login"].label = "email"
    self.fields["login"].widget.attrs.update(
        {
            "class": "some class",
            "placeholder": "",
            "type": "text",
            "name": "email",
        }
    )

    # password field
    self.fields["password"].label = "password"
    self.fields["password"].widget.attrs.update(
        {
            "placeholder": "",
            "class": "some class",
            "type": "password",
            "name": "password",
        }
    )

in settings.py:在 settings.py 中:

ACCOUNT_FORMS = {
    "login": "users.forms.CustomLoginForm",
}

also, you can check the docs to see for yourself.另外,您可以自己查看文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM