简体   繁体   English

重定向到当前页面Django

[英]Redirection to the current page Django

I'm trying to create a login form in Django but I can't find how to fix such a bug. 我正在尝试在Django中创建一个登录表单,但找不到解决此类错误的方法。 I have a form and the view with redirect in return but the problem is that the form redirects me to the current page even though I defined the path of redirection. 我有一个表单和带有重定向返回的视图,但是问题是即使我定义了重定向路径,该表单也将我重定向到当前页面。 Maybe you have some ideas? 也许您有一些想法? I tried to google it but it didn't work. 我试图用谷歌搜索,但没有成功。 Here is my views.py: 这是我的views.py:

def login(request):
    if request.POST.get('submit') == 'sign_in':
        form = login_form(data=request.POST)
        pdb.set_trace()
        return redirect('/account')
        #if form.is_valid():

         #   return HttpResponseRedirect('/account')

    elif request.POST.get('submit') == 'sign_up':
        form = register_form(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/account')
    else:
        sign_in = login_form()
        sign_up = register_form()
        return render(request, 'login.html', {'login_form':sign_in, 'signup_form': sign_up})

def account(request):
    return HttpResponse('Account')

and my login.html: 和我的login.html:

<div class="form-holder">
        <form class="login-form" method="post" style="display: block;">
            {% csrf_token %}
            {{ login_form }}
            <!--{% for field in login_form %}-->
                <!--<div class="form-group">-->
                    <!--{{ login_form.errors }}-->
                    <!--{{ login_form.label_tag }}-->
                    <!--<br>-->
                    <!--&lt;!&ndash;{{ field }}&ndash;&gt;-->
                    <!--<input class="form-control" id="{{ field.id_for_label }}"-->
                           <!--placeholder="Enter {{ field.html_name }}" name="{{ field.html_name }}" >-->
                    <!--{% if field.help_text %}-->
                    <!--<p class="help">{{ field.help_text|safe }}</p>-->
                    <!--{% endif %}-->
                <!--</div>-->
            <!--{% endfor %}-->
            <!--<div class="form-group">-->
                <!--<label for="email">Email:</label>-->
                <!--<input type="email" class="form-control" id="email" placeholder="Enter email" name="email">-->
            <!--</div>-->
            <!--<div class="form-group">-->
                <!--<label for="pwd">Password:</label>-->
                <!--<input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd">-->
            <!--</div>-->

            <div class="row">
                <div class="col-sm-6 col-sm-offset-3">
                    <button type="submit" value="sign_in" class="btn btn-lg btn-primary">Login</button>
                </div>
            </div>
        </form>

Thank you! 谢谢!

The problem was that I didn't check 'action' attribute of my forms. 问题是我没有检查表单的“操作”属性。 So, <form class="login-form" method="post" action="/account" style="display: block;"> worked for me and my view redirect me the /account page correctly. 因此, <form class="login-form" method="post" action="/account" style="display: block;">对我有用,我的视图正确地重定向了/account页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM