經過兩天的互聯網挖掘,我想總結一下我發現的東西。
有幾個方法:
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__.py
和login_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__.py
和login_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
必須改變,以loginctx
或loginform
你也可以寫自己的形式或繼承AllAuth
並將其導入如上所示的context processor
或templatetag
。
這兩種方法都是基於 - >this solution < -
在所有3種方法重定向作品需要(用戶返回到前一個頁面,在登錄成功的情況下,否則重定向到原來的AllAuth
模板在site.com/account/login
) 。
以上所有書面都可以通過Signup實現。
另外我問一些人,如果錯誤的用戶名\密碼的情況下,而不是重定向到site.com/account/login
,一個命題是使用AJAX
,但目前這是我的知識。有關連接登錄信息的一些基本信息可以找到默認的AllAuth視圖 - >here < - 。如果任何人都可以實現它,或找到任何教程,請在這裏發佈。
你可以在帖子中包含你的看法嗎? –
@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
但你的觀點在哪裏?你是否使用SignupView作爲父類? –