简体   繁体   English

在模板中将id =“”添加到'HttpResponseRedirect(reverse('name_view'))'。 Django的

[英]Adding id = “” in the template to 'HttpResponseRedirect (reverse ('name_view'))'. Django

Can I add an id to my method 'HttpResponseRedirect (reverse ('name_view'))'? 我可以在我的方法'HttpResponseRedirect(reverse('name_view'))'中添加ID吗? I have a simple form, the user can see the message ('!All is good! :).') after sending it. 我有一个简单的表格,用户发送后可以看到消息(“!一切都很好!:)。”)。 But it is at the bottom of the page and the user is redirected to the top of the page. 但是它在页面的底部,并且用户被重定向到页面的顶部。 Can I do a redirection to the bottom of the page (after saving the form)? 保存表格后,我可以重定向到页面底部吗? If so, how to achieve it. 如果是这样,如何实现它。 Any help will be appreciated. 任何帮助将不胜感激。

views.py views.py

if request.method == 'POST':
    contact_form = ContactForm(request.POST)
    if contact_form.is_valid():
        contact_form.save()
        messages.success(request, '!All is good! :).')
        return HttpResponseRedirect(reverse('app:home'))

else:
    contact_form = ContactForm()

In my case, I am looking for something like: 就我而言,我正在寻找类似的东西:

return HttpResponseRedirect(reverse('app:home#message_sending_section'))

home.html home.html

<section class="module divider-top" id="message_sending_section">
[...]
                        {% if messages %}
                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                            {% for message in messages %}
                            {% if message.tags %}
                            <span class="alert-inner--icon"><i class="fas fa-check"></i></span>
                            <span class="alert-inner--text"></strong>{{ message.tags }}</strong></span>
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                            {% endif %}
                                {% if message.level == DEFAULT_MESSAGE_LEVELS.ERROR %}Important: {% endif %}
                                {{ message }}
                            {% endfor %}
                        </div>
                        {% endif %}
[...]
</section>

The anchor is not part of the URL pattern that is the argument to reverse , but is appended to the generated URL that is the result of that call. 锚点不是URL模式的一部分,URL模式是reverse的参数,而是附加到作为该调用结果的生成的URL上。 So: 所以:

return HttpResponseRedirect("{}#message_sending_section".format(reverse('app:home')))

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

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