gpt4 book ai didi

python - Django 表单的 HTTP 响应问题

转载 作者:太空宇宙 更新时间:2023-11-04 10:46:54 25 4
gpt4 key购买 nike


目前,我尝试为一个小型数据库制作一个搜索表单。

这是我的 models.py 文件的一部分:

from django.db import models
from django import forms
#...
class searchForm(forms.Form):
searchField = forms.CharField(max_length = 100)
#...

这是我的 views.py 文件的一部分:

from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponseRedirect
#...
def index(request):
template = loader.get_template('index.html')
context = Context({})
return HttpResponse(template.render(context))

def search(request):
if request.method == 'POST': # If the form has been submitted...
form = searchForm(request.POST)# A form bound to the POST data
if form.is_valid():
searchData = form.cleaned_data['searchField']
return HttpResponseRedirect('search.html') # Redirect after POST #???
else:
searchData = searchForm() # an unbound form

return render(request, 'search.html', {'form': form,}) #???
#...

这是我的 index.html 的一部分,我想在其中实现该表单:

<label for="Search">Search:</label>
<form action = "/search/" method = "post">
{% csrf_token %} {{ form.as_p }}
<input type = "submit" value = "Go" />
</form>

我正在尝试做的事情:
当我提交表单时,我想重定向到名为 search.html 的结果文件,从哪里开始,是显示搜索文本字段的输入。链接结构应该是这样的:
着陆页是:http://127.0.0.1:8000/
在提交表单之后:http://127.0.0.1:8000/search.html

我认为搜索方法可能有错误,我在其中用'???'标记了行。下一个问题是,我的搜索文本字段没有显示。

如果有人能给我一些建议,那就太好了。

谢谢,艾尔乔布索

最佳答案

首先:表单没有显示,因为正如您所说,您希望它出现在 index.html 中,但是 index View 没有传递任何表单到模板。在 search View 中,您将模板传递给表单。

如果您想要描述的行为,您应该像这样重新组织代码:

from django.shortcuts import render
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.template.context import RequestContext

#...
def index(request):
# this will only render the template with the form
searchData = searchForm() # an unbound form
return render_to_response(
'index.html',
context_instance=RequestContext(
request,{'form':searchData,}
)
)

def search(request):
if request.method == 'POST': # If the form has been submitted...
form = searchForm(request.POST)# A form bound to the POST data
if form.is_valid():
searchData = form.cleaned_data['searchField']
# do whatever you want to process the search with
# searchada, maybe populate some variable
return render_to_response(
'search.html',
context_instance=RequestContext(
request,{'form':searchData,} # maybe add here the populated variable with the search
)
)
else:
# request.GET, just show the unbound form
searchData = searchForm() # an unbound form

return render_to_response(
'search.html',
context_instance=RequestContext(
request,{'form':searchData,}
)
)

那么你的模板应该是:

index.html

<!-- is not good to have a label outside form -->
<label for="Search">Search:</label>
<form action = "/search/" method = "post">
{% csrf_token %} {{ form.as_p }}
<input type = "submit" value = "Go" />
</form>

并且该文本也包含在 search.html 模板中,因为您也在那里呈现表单。

我希望这能带来一些启示!

关于python - Django 表单的 HTTP 响应问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16732813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com