python - Django 表单的 HTTP 响应问题

标签 python django forms


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

这是我的 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/

相关文章:

javascript - 按下按钮时将 HTML 表单字段添加到现有表单而不重新加载

python - 如何优化/简化堆排序 Django 对象? (不能使用模块和数据库排序)

python - django "Exception Value: No module named urls"用于管理站点

javascript - 当表单无效时, Angular 验证呈现 ng-valid

python - Python 3.4 中的 `async for`

python - 不在 ListView 中时,django rest framework 添加字段

PHP 表单列表

python - 匹配 python 正则表达式中的嵌入换行符

python - 如何对坐标进行非规范化?

Python 认为我的 setup.py 创建的包是一个模块而不是 python 包