用户使用Django提交表单后,如何重定向到“谢谢您与我们联系"页面? [英] How can I redirect to a Thank you for contacting us page after user submits a form with Django?

查看:83
本文介绍了用户使用Django提交表单后,如何重定向到“谢谢您与我们联系"页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Django提交表单后,我需要将用户重定向到一个新页面,上面写着谢谢您与我们联系".

I need to redirect the user to a new page that says, "Thank you for contacting us" after submitting a form with Django.

我尝试使用HttpResponseRedirect(反向),但是我不知道如何将其实现到项目的所有必需文件中.

I have tried using HttpResponseRedirect, reverse but I don't know how to implement it to all of the necessary files of my project.

from django.shortcuts import render
from .forms import ContactForm
# Create your views here.
def contact(request):
    template = "contact.html"

    if request.method == "POST":
        form = ContactForm(request.POST)

        if form.is_valid():
            form.save()

    else:
        form = ContactForm()

    context = {
        "form": form,
    }
    return render(request, template, context)

当我单击提交"时,它将数据发送到Django数据库,因此该表单可以工作,但用户不知道,因为在单击提交"后,它不会重定向到谢谢"页面.

When I click submit, it sends the data to the Django database so the form does work but the user does not know that because after they click submit it doesn't redirect to a thank you page.

推荐答案

只需使用新模板构建另一个视图(如您已有的视图),然后将其连接到新的url.然后将用户重定向到该URL.像这样:

Just build another view like the one you already have, with a new template and connect it to a new url. Then redirect the user to that url. Something like this:

from django.conf.urls import url
from .views import thank_you
urlpatterns = [
    url(r'^thanks/$', thank_you, name='thank-you'),
]

在您的views.py中:

来自django.shortcuts的

in your views.py:

from django.shortcuts import render

def thank_you(request):
    template = 'thank_you.html'
    context = {}
    return render(request, template, context)

这篇关于用户使用Django提交表单后,如何重定向到“谢谢您与我们联系"页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆