在成功URL重定向不适用于django-allauth中的自定义视图

时间:2013-03-15 19:33:55

标签: django django-views django-allauth

我已经创建了自己的类LocalSignup,它使用我自己的表单和我自己的验证逻辑扩展了基本的django-allauth SignupView类。问题是,在成功时我无法将视图重定向到除settings.py中的django LOGIN_REDIRECT_URL之外的任何内容。我在基于班级的视图中添加了success_url字段;我试过覆盖get_success_url函数(见下文);我还尝试使用LocalSignup.as_view(success_url="/add_account_select/")传递成功重定向。没有人工作过。关于我做错了什么想法?

class LocalSignup(SignupView):
    form_class = AllAuthUserForm
    template_name = 'signup/social_login.html'
    success_url = '/add_account_select/'  #reverse_lazy('add_account_select')    

    def get_success_url(self):
        return '/add_account_select/'  #reverse('add_account_select')

    def dispatch(self, request, *args, **kwargs):
        # self.sociallogin = request.session.get('socialaccount_sociallogin')
        return super(LocalSignup, self).dispatch(request, *args, **kwargs)

    def form_valid(self, form):
        ret = super(LocalSignup, self).form_valid(form)
        ### MY FORM VALIDATION LOGIC
        return ret

    def get_context_data(self, **kwargs):
        ret = super(LocalSignup, self).get_context_data(**kwargs)
        ret.update(dict(username=self.sociallogin.account.user.first_name,
                        photo_url=self.sociallogin.account.get_avatar_url() ))
        return ret

2 个答案:

答案 0 :(得分:1)

在默认视图中,如果有" next"在登录视图中的字段中,allauth会自动重定向到它。

<input id="login_redirect" type="hidden" name="next" value="#" />

完整表单

      <form method="POST" action="{% url "account_login" %}">
        {% csrf_token %}
        {{form}}
        <input  type="hidden" name="next" value="/add_account_select/" />
        <button type="submit" class="btn btn-primary">Sign IN</button>
      </form>

答案 1 :(得分:0)

只是提供最新答案:

使用django-allauth = 0.30.0,以下内容适用于views.py

from allauth.account.views import SignupView


class LocaLSignupView(SignupView):
    success_url = '/page/to/redirect/to'