Allauth login on homepage not working

此生再无相见时 提交于 2019-12-25 08:28:52

问题


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>

...

回答1:


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.



来源:https://stackoverflow.com/questions/41473047/allauth-login-on-homepage-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!