Allauth登录主页不工作 [英] Allauth login on homepage not working

查看:140
本文介绍了Allauth登录主页不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了让Django的allauth能够在我的主页上工作,我将登录和注册表单的allauth HTML复制到我自己的base.html(登录表单可以看到 here )。





我的网址和视图:



urls.py

  urlpatterns = [
url(r'^ $',views box_view,name ='news'),
url(r'^ accounts /',include('allauth.urls')),
]
pre>

views.py

  def box_view(request):
search = request.GET.get('search')
posts = Post.objects.all()。filter(category = 1).order_by(' - date')
如果搜索:
posts = posts.filter(Q(title__icontains = search)| Q(content__icontains = search))
else:
posts = Post.objects.all() .filter(category = 1).order_by(' - date')

context = {'posts':posts,}
return render(request,'polls.html',context)

base.html

  ... 

{%load i18n%}
< form class =log在method =POSTaction ={%url'account_login'%}>
< div class =loginWrapper>
< div class =loginNested>
< div class =loginBox>
{%csrf_token%}
{{form.as_p}}
{%如果redirect_field_value%}
< input type =hiddenname ={{redirect_field_name}} value ={{redirect_field_value}}/>
{%endif%}
< a class =button secondaryActionhref ={%url'account_reset_password'%}> {%trans忘记密码? %}< / A>
< button class =primaryActiontype =submit> {%trans登录%}< / button>
< / div>
< / div>
< / div>
< / form>

...


解决方案

通过传递表单中的上下文来管理它来修复它:

  from allauth.account.forms import LoginForm,SignupForm 

allauth_login = LoginForm(request.POST或None)
allauth_signup = SignupForm(request.POST或None)


上下文= {
'posts':posts,
'allauth_login':allauth_login,
'allauth_signup':allauth_signup
}

然后替换 {{form.as_p}} {{allauth_login}} 在我的模板中:

 < form class =loginmethod =POSTaction ={%url'account_login' %}> 
< div class =loginWrapper>
< div class =loginNested>
< div class =loginBox>
{%csrf_token%}
{{allauth_login}}
{%如果redirect_field_value%}
< input type =hiddenname ={{redirect_field_name}} ={{redirect_field_value}}/>
{%endif%}
< a class =button secondaryActionhref ={%url'account_reset_password'%}> {%trans忘记密码? %}< / A>
< button class =primaryActiontype =submit> {%trans登录%}< / button>
< / div>
< / div>
< / div>
< / form>

此方法仅在正确输入凭据时有效。所以当正确输入表单成功提交并登录/注册。但是,当凭据输入不正确登录,或者用户名已经被注册等等时,它将提交表单,并将我重定向到/ accounts / login或/ accounts / signup。这与正常的allauth不同,在没有提交表单的情况下显示错误。如果有人可以告诉我如何显示出很大的错误,否则我会发布一个单独的问题。


To allow Django's allauth to work on my homepage, I copied the allauth HTML for the login and signup forms to my own base.html (The login form can be seen here).

So this successfully renders the form and its fields on my homepage. However, when I click submit on the login form, it just redirects me to allauth's /accounts/login URL, which renders the same form. Only after that does submitting the form work. It's the same with the signup form when I submit the signup form on my homepage, it redirects me to /accounts/signup and then the signup works after submitting again. How can I make the login/signup work initially from my homepage?

My urls and views:

urls.py

urlpatterns = [
    url(r'^$', views.boxes_view, name='news'),
    url(r'^accounts/', include('allauth.urls')),
]

views.py

def boxes_view(request):
   search = request.GET.get('search')
   posts = Post.objects.all().filter(category=1).order_by('-date')
   if search:
      posts = posts.filter(Q(title__icontains=search)|Q(content__icontains=search))
   else:
      posts = Post.objects.all().filter(category=1).order_by('-date')

   context = {'posts': posts,}
   return render(request, 'polls.html', context)

base.html

...

{% load i18n %}
<form class="login" method="POST" action="{% url 'account_login' %}">
<div class="loginWrapper">
    <div class="loginNested">
    <div class="loginBox">
  {% csrf_token %}
  {{ form.as_p }}
  {% if redirect_field_value %}
  <input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
  {% endif %}
  <a class="button secondaryAction" href="{% url 'account_reset_password' %}">{% trans "Forgot Password?" %}</a>
  <button class="primaryAction" type="submit">{% trans "Sign In" %}</button>
</div>
</div>
</div>
</form>

...

解决方案

Managed to fix it by passing in the forms as context in my view:

    from allauth.account.forms import LoginForm, SignupForm

    allauth_login = LoginForm(request.POST or None)
    allauth_signup = SignupForm(request.POST or None)


    context = {
        'posts': posts,
        'allauth_login': allauth_login,
        'allauth_signup': allauth_signup
    }

Then replacing {{ form.as_p }} for {{ allauth_login }} in my template:

<form class="login" method="POST" action="{% url 'account_login' %}">
<div class="loginWrapper">
    <div class="loginNested">
    <div class="loginBox">
  {% csrf_token %}
  {{ allauth_login }}
  {% if redirect_field_value %}
  <input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
  {% endif %}
  <a class="button secondaryAction" href="{% url 'account_reset_password' %}">{% trans "Forgot Password?" %}</a>
  <button class="primaryAction" type="submit">{% trans "Sign In" %}</button>
</div>
</div>
</div>
</form>

This method only works when the credentials are entered correctly. So when entered correctly the form successfully submits and logs in/signs up. However when credentials are entered incorrectly for login, or when a username is already taken for signup etc, it will submit the form anyway and redirect me to /accounts/login or /accounts/signup. This is different to the normal allauth which shows errors without submitting the form. If anyone could tell me how to show errors that would be great, otherwise I will post a seperate question.

这篇关于Allauth登录主页不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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