【问题标题】:Customized django all-auth form not submitting自定义的 django all-auth 表单未提交
【发布时间】:2021-02-04 07:32:54
【问题描述】:

我正在使用 django all-auth 登录表单。我想自定义表单字段的外观,所以我将帐户文件夹中的 login.html 更改为如下所示:

<form class="login" method="POST" action="{% url 'account_login' %}">
    {% csrf_token %}
    {% for field in form.visible_fields|slice:'2' %}
        <div class="row form-group">
            {% if field.name == 'login' %}
                <input type="text" placeholder="Email"><i class="fas fa-at"></i>
            {% else %}
                <input type="password" placeholder="Password"><i class="la la-lock"></i>
            {% endif %}
        </div>
    {% endfor %}
    <a href="{% url 'account_reset_password' %}">Forgot Password?</a>
    <button type="submit">Sign In</button>
</form>

表单完全按照我想要的方式呈现,但是当我单击提交时没有任何反应。对我来说奇怪的是,如果代替我的 for 循环,我只需键入 {{ form.as_p }},表单提交得非常好,它只是看起来不像我想要的那样。任何人都可以在我的循环中看到错误,或者这里还有其他问题。我一直在网上寻找解决方案,但一直没有成功

【问题讨论】:

    标签: django form-submit django-allauth django-custom-field


    【解决方案1】:

    您需要在输入标签中指定字段的名称,否则 POST 字典将为空。您正在使用{% if field.name == 'login' %},但您忘记指定名称属性。同样适用于密码输入。

    <form class="login" method="POST" action="{% url 'account_login' %}">
        {% csrf_token %}
        {% for field in form.visible_fields|slice:'2' %}
            <div class="row form-group">
                {% if field.name == 'login' %}
                    <input name='login' type="text" placeholder="Email"><i class="fas fa-at"></i>
                {% else %}
                    <input name='password' type="password" placeholder="Password"><i class="la la-lock"></i>
                {% endif %}
            </div>
        {% endfor %}
        <a href="{% url 'account_reset_password' %}">Forgot Password?</a>
        <button type="submit">Sign In</button>
    </form>
    

    【讨论】:

    • 是的,做到了。非常感谢
    猜你喜欢
    • 2016-03-09
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    相关资源
    最近更新 更多