我试图有一个简单的表格,一旦填写,将定向到不同的网页或如果无效则保留在同一页面上。该页面应该有一个文本框并提交表单,一旦用户输入任何内容,它就会将您定向到一个单独的页面。
我的目录结构如下:
appName/
app/
forms.py
urls.py
views.py
templates/
app/
goodbye.html
name.html
library.html
thanks.html
appName/
settings.py
urls.py
我的 app/urls.py 如下:
from django.conf.urls import url
from . import views
app_name = 'app'
urlpatterns = [
url(r'^$', views.index2, name = 'index'),
url(r'^hello/$', views.hello, name = 'hello'),
url(r'^goodbye/$', views.goodbye, name = 'goodbye'),
#url(r'^library$', views.library, name = 'library'),
url(r'^library/$', views.library, name = 'library'),
url(r'^library/(?P<book_id>[0-9]+)/$', views.book, name = 'book'),
url(r'^getname/$', views.get_name, name = 'get_name'),
url(r'^your-name/$',views.get_name, name='get_name'),
url(r'^thanks/$',views.say_thanks,name='thanks'),
#url(r'^thanks/(?P<name_id>[a-zA-Z]+)/$', views.say_thanks,name='thanks'),
]
我的 forms.py 是:
从 Django 导入表单
class NameForm(forms.Form):
your_name = forms.CharField(label = 'Your name', max_length=100)
我的 app/views.py 是:
from django.http import HttpResponse
from django.template import loader
from django.shortcuts import render
from django.http import HttpResponseRedirect
#forms
from .forms import NameForm
# Create your views here.
def index2(request):
return HttpResponse("hello world")
def hello(request):
text = """<h1>Welcome to my app! </h1>"""
return HttpResponse(text)
def goodbye(request):
template = loader.get_template("app/goodbye.html")
context = {
'output' : 'This is output from goodby views.py request handler'
}
return HttpResponse(template.render(context,request))
def library(request):
template = loader.get_template("app/library.html")
context = {
'output' : 'Welcome to the libary!!'
}
return HttpResponse(template.render(context, request))
def book(request, book_id):
return HttpResponse("You're looking at book %s. " % book_id)
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
locationGo = "/thanks/"
template = loader.get_template("app/thanks.html")
return HttpResponse(template.render({'name':'name'},request))
else:
form = NameForm()
template = loader.get_template("app/name.html")
context = {'form': form}
return HttpResponse(template.render(context, request))
def say_thanks(request):
template = loader.get_template("app/thanks.html")
return HttpResponse(template.render({'name': 'name'},request))
我的模板包括:
名称.html :
<form action = "/getname/" method = "post">
{% csrf_token %}
{{ form }}
<input type = "submit" value = "Submit" />
</form>
再见.html
<h1>Goodbye to Template Romance</h1>
<a href="{% url 'app:index' %}">Go Back</a>
Thanks.html
Thanks {{name}}!
我想要的是:
在 get_name(request): 函数中,POST 和 GET if...else 似乎没有根据 Submit 按钮触发,并且它似乎没有加载正确的页面,或者更改当前的 URL 地址一旦它被处理。我曾尝试使用 HttpRedirect() 工作,但是,我也想传递表单数据(这是另一个问题)。
任何建议都会有很大帮助!
最佳答案
您的第一个问题是当请求方法为 post 且表单无效时,您没有返回响应。您可以通过更改 View 的缩进来解决该问题,以便始终在 View 结束时返回响应。
def get_name(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
...
else:
form = NameForm()
template = loader.get_template("app/name.html")
context = {'form': form}
return HttpResponse(template.render(context, request))
如果要重定向到
/thanks/
View ,则可以使用重定向快捷方式。 if form.is_valid():
return redirect('thanks')
请注意,无法重定向和传递表单数据(有关说明,请参阅 this question)。在重定向之前,您应该对数据进行任何您想要的处理。您可以使用 messages framework 在重定向之前创建消息
'Thanks <name>'
。这是有效的,因为您的 url 模式中有
name='thanks'
。您可以使用
render
快捷方式来简化 View 。代替template = loader.get_template("app/name.html")
context = {'form': form}
return HttpResponse(template.render(context, request))
你可以简单地做:
return render(request, "app/name.html", context)
请记住为快捷方式添加导入:
from django.shortcuts import redirect, render
关于python - DJANGO - 使用数据从 POST 重定向到不同的页面,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36578239/