我想在提交表单时重定向另一个URL。
提交后,我希望创建另一个页面,其中包含“ Hello(SummonerName)和Welcome”之类的URL,例如:interface / main / SummonerName =(SummonerName)
这是我的代码:
forms.py
from django import forms
class SummonerForm(forms.Form):
summoner_name = form.CharField(label='SummonerName:', max_length=100)
views.py
from django.http import HttpresponseRedirect
from django.shortcuts import render
from .forms import NameForm
def get_name(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = NameForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/interface/name.html')
# if a GET (or any other method) we'll create a blank form
else:
form = NameForm()
return render(request, 'interface/main.html', {'form': form})
和我的main.html
<form action="/SummonerName={{ current_name }}" method="post">
<label for="summoner_name">SummonerName: </label>
<input id="summoner_name" type="text" name="summoner_name" value="{{ current_name }}">
<input type="submit" value="Rechercher">
</form>
编辑:
urls.py
from django.urls import path
from . import views
urlpatterns = {
path('main', view.get_summoner_name)
}
谢谢!
答案 0 :(得分:0)
return HttpResponseRedirect
就是您的views.py。您只需要传递所需的URL作为第一个参数,而不是`'/interface/name.html'。
请参阅: https://docs.djangoproject.com/en/2.2/ref/request-response/#httpresponse-subclasses
要传递名称,您有很多选择,具体取决于您要对输入的数据进行处理。
一个简单的解决方案:
您的示例非常简单,因此,如果您只想重定向到某些URL并显示用户输入的名称,最简单的方法就是通过URL作为查询字符串传递名称(以及其他任何参数):>
import urllib.parse
# ... omitted code
def get_name(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = NameForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# get parameters from the form
params = {
"summoner_name" : form.validated_data["summoner_name"],
# multiple parameters can be passed in this dict e.g.
# "some_other param" : param_value
}
# encode the dictionary into a querystring
q_string = urllib.parse.urlencode(params)
# construct the new URL:
redirect_url = "/interface/success/?summoner_name={}".format(q_string)
return HttpResponseRedirect(redirect_url)
# if a GET (or any other method) we'll create a blank form
else:
form = NameForm()
return render(request, 'interface/main.html', {'form': form})
然后您可以通过请求对象在视图/模板中访问这些参数。
然后,在您的urls.py中,您应该通过在模式中添加以下内容来匹配此url模式:
path('interface/success/', views.success_view),
您当然应该定义该视图:
# views.py
def success_view(request):
return render(request, "success.html")
最后,您应该添加模板success.html
。在那里,您可以<h1> Hello {{ request.GET.summoner_name }}! </h1>
更常见的用例
上面的方法仅显示用户输入的数据,这通常不是很有用。...通常,以一种形式,我们询问需要保存到db的数据,这就是为什么我在评论中提出了ModelForm的原因。简而言之,它不能解释确切的工作方式,这超出了此答案的范围:您应该在models.py(即db表)中定义一个可以存储数据的模型(summoner_name是这样一个字段)。型号)。
class SimpleModel(models.Model):
summoner_name=models.CharField(max_length=256)
# other fields you may need
然后,您可以使用基于django的通用类的视图,例如
class SimpleFormView(CreateView):
model = SimpleModel
success_url = "interface/success"
urls.py也将需要进行一些编辑,并且您的模板也应遵循Django的命名约定。如果您想这样下去,请阅读以下内容: https://docs.djangoproject.com/en/2.2/topics/class-based-views/generic-editing/#form-handling-with-class-based-views