按钮同时充当 "submit" 和 "href"
Button acts as both "submit" and "href"
我创建了一个表单,然后我想创建一个同时充当 "submit" 和 "href" 的按钮。这意味着当我点击按钮时,它会提交表单并重定向到另一个页面。
当前页面:“http://127.0.0.1:8000/”
提交后重定向页面:“http://127.0.0.1:8000/polls/”
我试图在表单上添加一个 link 到操作(代码如下),但它不起作用。它只是重定向到 link 而没有提交任何东西。
<form method='POST' action='http://127.0.0.1:8000/polls/' >{% csrf_token %}
<p>Questionnaire</P>
{{form.as_p}}
<input type="submit" value="Submit & Next Page"/>
</form>
我的views.py:
from django.shortcuts import render
from .forms import SurveyForm
def homepage(request):
title = "Questionnaire"
form = SurveyForm(request.POST or None)
if form.is_valid():
instance = form.save(commit=False)
instance.save()
context = {
"title": title,
"form": form,
}
return render(request, "homepage.html", context)
如果有人能帮助我,我将不胜感激。
谢谢。
"normal"方式是将POST表单到你的视图中(这里使用action
link),在这个视图函数中,你重定向到另一个URL当表格全部正确时。
你通常有这个代码:
if request.method == "POST":
form = YourForm(request.POST)
if form.is_valid():
# do stuff
return HttpResponseRedirect(reverse(poll_url))
else:
form = YourForm(request.POST)
return render(request, <html file>, {'form': form})
您不能从一个动作中执行两个动作 button/link。您有两个解决方案:
1\重定向:
在 http://127.0.0.1/
上将您的表单设置为 POST
。在服务器端,您处理您的表单并发送一个 http 301 response (Redirect).
2\ 发送到 /polls :
将类似的所有内容保留在您的表格中。但是在服务器端,将您的处理从处理转移到将呈现页面的相同方法。
这样,您的方法就会处理表单,然后显示下一页。
您可以采用两种方法(首选第一种):
首先-在你看来,应该是这样的:
def view(request):
if request.method == 'POST':
form = SomeForm(request.POST)
if form.is_valid():
# Do something
# Add the line below to redirect to the name of a url
# after completion of the form
return HttpResponseRedirect(reverse('polls'))
第二 - 您可以尝试将 onclick="window.location='http://127.0.0.1:8000/polls/'"
添加到您的按钮。那应该将它重定向到新的 window。例如:
<form method='POST' onclick="window.location='http://127.0.0.1:8000/polls/'">{% csrf_token %}
<p>Questionnaire</P>
{{form.as_p}}
<input type="submit" value="Submit & Next Page"/>
</form>
注意:如果您选择第二种方法(可能不应该),明智的做法是为 url 使用 Django 模板(即 {% url 'polls' %} 而不是 http://127.0.0.1:8000/polls/)
希望对您有所帮助!
我创建了一个表单,然后我想创建一个同时充当 "submit" 和 "href" 的按钮。这意味着当我点击按钮时,它会提交表单并重定向到另一个页面。
当前页面:“http://127.0.0.1:8000/” 提交后重定向页面:“http://127.0.0.1:8000/polls/”
我试图在表单上添加一个 link 到操作(代码如下),但它不起作用。它只是重定向到 link 而没有提交任何东西。
<form method='POST' action='http://127.0.0.1:8000/polls/' >{% csrf_token %}
<p>Questionnaire</P>
{{form.as_p}}
<input type="submit" value="Submit & Next Page"/>
</form>
我的views.py:
from django.shortcuts import render
from .forms import SurveyForm
def homepage(request):
title = "Questionnaire"
form = SurveyForm(request.POST or None)
if form.is_valid():
instance = form.save(commit=False)
instance.save()
context = {
"title": title,
"form": form,
}
return render(request, "homepage.html", context)
如果有人能帮助我,我将不胜感激。 谢谢。
"normal"方式是将POST表单到你的视图中(这里使用action
link),在这个视图函数中,你重定向到另一个URL当表格全部正确时。
你通常有这个代码:
if request.method == "POST":
form = YourForm(request.POST)
if form.is_valid():
# do stuff
return HttpResponseRedirect(reverse(poll_url))
else:
form = YourForm(request.POST)
return render(request, <html file>, {'form': form})
您不能从一个动作中执行两个动作 button/link。您有两个解决方案:
1\重定向:
在 http://127.0.0.1/
上将您的表单设置为 POST
。在服务器端,您处理您的表单并发送一个 http 301 response (Redirect).
2\ 发送到 /polls :
将类似的所有内容保留在您的表格中。但是在服务器端,将您的处理从处理转移到将呈现页面的相同方法。 这样,您的方法就会处理表单,然后显示下一页。
您可以采用两种方法(首选第一种):
首先-在你看来,应该是这样的:
def view(request):
if request.method == 'POST':
form = SomeForm(request.POST)
if form.is_valid():
# Do something
# Add the line below to redirect to the name of a url
# after completion of the form
return HttpResponseRedirect(reverse('polls'))
第二 - 您可以尝试将 onclick="window.location='http://127.0.0.1:8000/polls/'"
添加到您的按钮。那应该将它重定向到新的 window。例如:
<form method='POST' onclick="window.location='http://127.0.0.1:8000/polls/'">{% csrf_token %}
<p>Questionnaire</P>
{{form.as_p}}
<input type="submit" value="Submit & Next Page"/>
</form>
注意:如果您选择第二种方法(可能不应该),明智的做法是为 url 使用 Django 模板(即 {% url 'polls' %} 而不是 http://127.0.0.1:8000/polls/)
希望对您有所帮助!