我有一个登录页面,除了重定向到推荐人页面外,它工作正常.用户在应用程序中收到一封带有直接链接的邮箱,他们(在本例中)尚未登录,并被重定向到登录页面.成功登录后,用户将被重定向到硬编码路径.见下面的例子.

邮箱中的URL:http://localhost:8000/issueapp/1628/view/22

登录页面的URL:http://localhost:8000/login?next=/issueapp/1628/view/22

登录视图(带有硬编码重定向):

def login_user(request):    
    state = "Please log in below..."
    username = password = ''

    if request.POST:
        username = request.POST['username']
        password = request.POST['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('/issueapp/1628/')
            else:
                state = "Your account is not active, please contact the site admin."
        else:
            state = "Your username and/or password were incorrect."

    return render_to_response(
        'account_login.html',
        {
        'state':state,
        'username': username
        },
        context_instance=RequestContext(request)
    )

登录视图(带有"下一步"重定向):

def login_user(request):    
    state = "Please log in below..."
    username = password = ''

    if request.POST:
        username = request.POST['username']
        password = request.POST['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.GET['next'])
            else:
                state = "Your account is not active, please contact the site admin."
        else:
            state = "Your username and/or password were incorrect."

    return render_to_response(
        'account_login.html',
        {
        'state':state,
        'username': username
        },
        context_instance=RequestContext(request)
    )

上述视图导致异常"Key 'next' not found in <QueryDict: {}>"表单似乎没有张贴"NEXT"变量,即使它在URL和表单中. 我到处找了又找,都想不出为什么不管用.有什么主意吗?

Additional reference:

登录模板:

{% block content %}

    {{ state }}
    <form action="/login/" method="post" >
                {% csrf_token %}
        {% if next %}
        <input type="hidden" name="next" value="{{ next }}" />
        {% endif %}
        username:
        <input type="text" name="username" value="{{ username }}" /><br />
        password:
        <input type="password" name="password" value="" /><br />

        <input type="submit" value="Log In"/>

        {% debug %}
    </form>
{% endblock %}

EDIT: The below is the code which is now working for me (thanks to the help of Paulo Bu)! **

登录视图:

def login_user(request):

    state = "Please log in below..."
    username = password = ''

    next = ""

    if request.GET:  
        next = request.GET['next']

    if request.POST:
        username = request.POST['username']
        password = request.POST['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!"
                if next == "":
                    return HttpResponseRedirect('/issueapp/')
                else:
                    return HttpResponseRedirect(next)
            else:
                state = "Your account is not active, please contact the site admin."
        else:
            state = "Your username and/or password were incorrect."

    return render_to_response(
        'account_login.html',
        {
        'state':state,
        'username': username,
        'next':next,
        },
        context_instance=RequestContext(request)
    )

登录模板:

{{ state }}

{% if next %}
<form action="/login/?next={{next}}" method="post" >
{%else%}
<form action="/login/" method="post" >
{% endif %}

            {% csrf_token %}

    username:
    <input type="text" name="username" value="{{ username }}" /><br />
    password:
    <input type="password" name="password" value="" /><br />

    <input type="submit" value="Log In"/>

    {% debug %}
</form>

推荐答案

您的代码很好,唯一的问题是,在表单中,您将next属性作为post传递,因为方法是post.在视图中,您试图获取get字典中的next参数,这显然不存在.

您必须像这样声明html表单action才能使视图工作.

{% if next %}
<form action="/login/?next={{next}}" method="post" >
{%else%}
<form action="/login/" method="post" >
{% endif %}
        {% csrf_token %}
        username:
        <input type="text" name="username" value="{{ username }}" /><br />
        password:
        <input type="password" name="password" value="" /><br />

        <input type="submit" value="Log In"/>

        {% debug %}
    </form>

在那里,如果有一个next变量,那么您可以在url中将其作为GET参数进行检索.如果没有,则表单不包括它.

这是最好的方法,但您也可以在视图中通过请求POST字典中的next来修复此问题,如下所示:

return HttpResponseRedirect(request.POST.get('next'))

注意,这仅在模板account_login has是名为next的变量时才起作用.您应该在视图中生成它,并在呈现它时将其传递给模板.

通常,在模板中,您将执行如下操作:

# this would be hardcoded
next = '/issueapp/1628/view/22'
# you may add some logic to generate it as you need.

然后你要做的就是:

return render_to_response(
    'account_login.html',
    {
    'state':state,
    'username': username,
    'next':next
    },
    context_instance=RequestContext(request)
)

希望这有帮助!

Django相关问答推荐

使用override_sets构建一个预装饰的类,以更快的客户端.登录?

自定义公钥打破Django管理内联逻辑

Django相关对象引用模板

基于Django类的视图:除非登录,否则拒绝访问future 日期

django re_path() 函数模式与包含该模式的 url 不匹配

来自日历的 Django 动态 url

如何删除django请求中的重复项

如何将 ManyToManyField 与另一个 ManyToManyField 进行比较

删除所有实例后,Django 模型实例主键不会重置为 1

如何在 Django 中向 ModelForm 添加外键字段?

Django populate() 不可重入

始终将用户包含在 django 模板上下文中

Django表单集:首先需要?

如何使 Django QuerySet 批量删除()更高效

使用 get_object_or_404 获取数据库值

如何重置 PostgreSQL 表上的 ID 序列

Django Admin - save_model 方法 - 如何检测字段是否已更改?

如何在 Django 中创建一个非空的 CharField?

在 Django 单元测试中使用 mock 修补 celery 任务

Django:按位置排序,忽略 NULL