2016-08-29 44 views
0

我有一個固定的導航,我想添加下拉框,用戶可以singup \(作爲Twitter使用)。如何將singup signin模板移動到下拉菜單中?

我想:

# project/tempates/signup.html 
{% load i18n %} 
{% load account socialaccount %} 

{% block head_title %}{% trans "Signup" %}{% endblock %} 

{% block content %} 

    <h1>{% trans "Sign Up" %}</h1> 

    <p>{% blocktrans %}Already have an account? Then please <a href="{{ login_url }}">sign in</a>.{% endblocktrans %}</p> 

    <form class="signup" id="signup_form" method="post" action="{% url 'account_signup' %}"> 
    {% csrf_token %} 
    {{ signupform.as_p }} 
    {% if redirect_field_value %} 
    <input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" /> 
    {% endif %} 
    <button type="submit">{% trans "Sign Up" %} &raquo;</button> 
    </form> 

{% endblock %} 

# project/tempates/base.html 
# ... a lot of basic stuff 
<li class="dropdown"> 
    <a class="dropdown-toggle" href="#" data-toggle="dropdown">Sign In <strong class="caret"></strong></a> 
    <div class="dropdown-menu" style="padding: 15px; padding-bottom: 0px;"> 
     {% include './signup.html' %} 
# ... rest stuff 

並在下拉框中我看到的只是文本,鏈接,簽到,並登記確認按鈕。

有沒有領域可以輸入電子郵件和密碼。據我所知,這是因爲沒有訪問表單,通常是一個意見的工作。我怎樣才能得到可行的下拉表單?

+0

你可以在帖子中包含你的看法嗎? –

+0

@MichaelFourre,這裏似乎有一個問題。我發現在SO(例如 - http://stackoverflow.com/questions/29499449/django-allauth-login-signup-form-on-homepage)和互聯網(http://notesbyanerd.com/joint-login-and -signup-django-allauth-view)一些意見樣本,但我不知道如何將它們連接到下拉表單。原創意見在這裏 - https://github.com/pennersr/django-allauth/blob/master/allauth/account/views.py – TitanFighter

+0

但你的觀點在哪裏?你是否使用SignupView作爲父類? –

回答

3

經過兩天的互聯網挖掘,我想總結一下我發現的東西。

有幾個方法:

1.使用<form action='some address here'>。最簡單的方法。

要檢查默認AllAuth形式,我們需要:

# ./manage.py shell 
>>> import allauth.account.forms as forms 
>>> f = forms.LoginForm() 
>>> print(f) 

下面是編輯其直接加入到base.html文件

方法是基於解決方案的print(f)版本 - >here < -

2. Contex處理器

a)使文件夾your_project/your_app/context_processor。將有2個文件 - __init__.pylogin_ctx.py

B)login_ctx.py附加:

from allauth.account.forms import LoginForm 

def login_ctx_tag(request): 
    return {'loginctx': LoginForm()} 

C)在項目SETTINGS添加your_app.context_processors.login_ctx.login_form_ctx' in TEMPLATES`部分。喜歡的東西:

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [os.path.join(BASE_DIR, 'templates'), os.path.join(BASE_DIR, 'templates', 'allauth')], 
     'APP_DIRS': True, 
     'OPTIONS': { 
      'debug': DEBUG, 
      'context_processors': [ 
       'your_app.context_processors.login_ctx.login_form_ctx', # <- put your processor here 
       'django.template.context_processors.debug', 
       # [...other processors...] 
      ], 
     }, 
    }, 
] 

d)在你*.html在您需要添加下一個:

{% if not user.is_authenticated %} 
    <form action="{% url 'account_login' %}" method="post"> 
     {% csrf_token %} 
     <input type="hidden" name="next" value="{{ request.get_full_path }}" /> 
     {{ loginctx }} 
     <button type="submit">Login</button> 
    </form> 
{% else %} 
    {# display something else here... (username?) #} 
{% endif %} 

3模板標籤

一)新建文件夾your_project/your_app/templatetags。將有2個文件 - __init__.pylogin_tag.py

B)login_tag.py附加:

from django import template 
from allauth.account.forms import LoginForm 

register = template.Library() 

@register.inclusion_tag('profiles/true_login.html') 
def login_form_tag(current_page=None): 
    return {'loginform': LoginForm(), 
      'redirect_to': current_page} 

C)your_project/your_app/templates/your_app/ make文件login_form.html的內容:

{% load account %} 

{% if not user.is_authenticated %} 
    <form action="{% url 'account_login' %}" method="post"> 
     {% csrf_token %} 
     <input type="hidden" name="next" value="{{ redirect_to }}" /> 
     {{ loginform }} 
     <button type="submit">Login</button> 
    </form> 
{% else %} 
    {# display something else here... (username?) #} 
{% endif %} 

d)在任何*.html你需要,添加在頂部{% load login_tag %}並在需要的地方添加{% login_form_tag request.get_full_path %}


的第二和第三個方法表明本地AllAuth形式。如果您需要使用{{form}}以某種方式對其進行編輯, - >here < - 在文檔中,您可以找到一些示例說明如何執行此操作。要提的是,如果在文檔顯示是這樣的:

<div class="fieldWrapper"> 
    {{ form.subject.errors }} 
    {{ form.subject.label_tag }} 
    {{ form.subject }} 
</div> 

我們的案例form必須改變,以loginctxloginform

你也可以寫自己的形式或繼承AllAuth並將其導入如上所示的context processortemplatetag

這兩種方法都是基於 - >this solution < -

在所有3種方法重定向作品需要(用戶返回到前一個頁面,在登錄成功的情況下,否則重定向到原來的AllAuth模板在site.com/account/login) 。

以上所有書面都可以通過Signup實現。

另外我問一些人,如果錯誤的用戶名\密碼的情況下,而不是重定向到site.com/account/login,一個命題是使用AJAX,但目前這是我的知識。有關連接登錄信息的一些基本信息可以找到默認的AllAuth視圖 - >here < - 。如果任何人都可以實現它,或找到任何教程,請在這裏發佈。