1


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

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

我认为搜索方法可能有错误,我用'???'标记了这些行。下一个问题是,我的搜索文本字段没有出现。

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

谢谢,埃尔约索

4

2 回答 2

3

首先:表单没有显示,因为正如您所说,您希望它出现,index.htmlindex视图没有将任何表单传递给模板。是在search您将表单传递给模板的地方。

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

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,}
        )
    )

那么你的模板应该是:

索引.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模板中,因为您也可以在那里呈现表单。

我希望这可以带来一些光明!

于 2013-05-24T13:05:59.280 回答
1

使用 django FormView你可以这样做:

class Index(FormView):
    form_class = SearchForm
    template_name = 'index.html'
    success_template = 'search.html' # I've added this attr

    def form_valid(self, form): #That return a your form, validated
      # Here you can do something with you VALID form.
      searchData = form.cleaned_data['searchField']
      context = dict(
            searchData=searchData,
        )
      return render_to_response(self.success_template, {}, RequestContext(self.request, context))
于 2013-05-24T13:18:47.143 回答