0

我为登录表单制作了一个包含标签,它将显示在每个页面中。但是成功登录后我无法重定向页面。

这是我的代码:

from django import template
from django.contrib.auth import authenticate, login
from django.http.response import HttpResponseRedirect

register = template.Library()

def costumlogin(context):
    request = context['request']
    if request.user.is_anonymous():
        state = "Please log in below..."
        username = password = ''
        if 'mylogin' in request.POST:
            username = request.POST.get('username')
            password = request.POST.get('password')

            user = authenticate(username=username, password=password)
            if user is not None:
                if user.is_active:
                    login(request, user)
                    state = "You're successfully logged in!"
                    return HttpResponseRedirect(request.META.get('HTTP_REFERER','/'))
                else:
                   state = "Your account is not active, please contact the site admin."
           else:
                state = "Your username and/or password were incorrect."
        return {'username':username, "state": state}

register.inclusion_tag('registration/login.html',takes_context = True)(costumlogin)
4

1 回答 1

1

你不能这样做,这是没有意义的。只有视图可以返回响应,包括重定向。

模板标签不是处理表单的合适位置。您应该将表单发布到不同的 URL 并在该视图中进行处理。

于 2013-10-25T07:31:16.537 回答