登录/直接在主页上注册 [英] Log in / Sign up directly on home page

查看:121
本文介绍了登录/直接在主页上注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

而不是登录页面../account/login/,我想使用户可以直接在主页上进行登录。我应该怎么做才能使之成为可能?如何使用allauth连接主页上的输入字段?我不知道这是不是太复杂了,以这样的方式工作?

Instead of logging in on page '../account/login/' i would like to make avaliable for users to log directly on home page for example. What should i do to make it possible? How to connect input fields on home page with allauth? I don't know if this is too complicated to make it work in such way?

推荐答案

有一个解决方案适用于我。这不是最好的解决方案,因为它不是干的,所以我会感激有人改进它!

There is a solution that works for me. It's not the optimal solution because it's not DRY, so I would be grateful is somebody improves it!

基本上我做的是渲染官方的allauth登录模板并复制表单的HTML代码。然后我更改了csrf令牌和URL,并将其粘贴到主页模板。

Basically what I did is to render the official allauth login template and copy the HTML code of the form. Then I changed the csrf token and the urls and pasted it to the home page template.

以下是登录/注销的示例代码。您可以在注册时做同样的事情。 (请注意,我仅使用电子邮件,而不是用户名登录,您的代码可能会根据您的allauth设置而有所不同,请查看您的登录和注册模板,以查看您需要的输入字段及其名称。)

Here is the example code for login/logout. You can do the same with sign up. (Note that I'm using e-mail only and not username to login, your code might be different based on your allauth settings. Take a look at your login and signup templates to see what input fields do you need and its names.)

{% load account %}

<h1>Login / Logout</h1>

{% if user.is_authenticated %}
    <p>Loged in with e-mail: {{ request.user.email }}</p>
    <a href="{% url "account_logout" %}">Logout</a>
{% else %}
    <form action="{% url "account_login" %}" method="post">
        {% csrf_token %}
        <input type="email" placeholder="E-mail" name="login">
        <input type="password" placeholder="Password" name="password">
        <label for="id_remember_menu" class="text-primary">Remember Me:</label>
        <input id="id_remember_menu" name="remember" type="checkbox">
        {% if redirect_field_value %}
            <input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
        {% endif %}
        <button type="submit">Login</button>
        <a href="{% url 'account_reset_password' %}">Forgot Password?</a>
    </form>
{% endif %}

我想改进代码并能够呈现该表单直接在主页模板中,而不是复制HTML,而是使用 {{form}} 如果我设法做到这一点,我稍后会更新答复。

I would like to improve the code and be able to render the form directly in the home page template with {{ form }} instead of copying the HTML. If I manage to do it I will update the reply later.

这篇关于登录/直接在主页上注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆