Django表单没有出现在Template中

时间:2014-05-11 04:35:06

标签: python django

我的表单与搜索结果页面显示在同一页面上。

表单类如下:

class MailForm(forms.Form):
    toEmail = forms.EmailField(widget=forms.TextInput(attrs={'class': 'form-control input-area',
                                                        'placeholder': 'Enter the mail ID to whom you want to send this'}))
    fromName = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control input-area',
                                                         'placeholder': 'Enter your name'}))
    message = forms.Textarea()

我将其渲染为Bootstrap模式,如下所示:

{% if form.errors %}
    <p>Please correct the {{ form.errors|pluralize }} below.</p>
{% endif %}
<form action="" method="post">
{{ form.as_table }}
{% csrf_token %}
<input type="submit" value="Send">
</form>

views.py

def search_page_form(request):
    if request.method == 'POST':
        forms = MailForm(request.POST)
        if forms.is_valid():
            cd = forms.cleaned_data
            send_mail(
                cd['toEmail'],
                cd['fromName'],
                cd['message'],
                cd.get('email', 'noreply@shp.com'),
                ['toEmail']
            )
    else:
        forms = MailForm()
    return render_to_response('templates/search.html', {'form': forms})

我认为问题在于我也在同一页面上呈现搜索结果,如下所示:

def search_page(request):
    if 'query' in request.GET and request.GET['query']:
        city = request.GET['city']
        q = request.GET['query']
        ct = Classified.objects.filter(name__icontains=q).filter(address__city__name__exact=city).filter(active__exact='yes')
        paginate = Paginator(ct, 10)
        page = request.GET.get('page')
        try:
            ct = paginate.page(page)
        except PageNotAnInteger:
            ct = paginate.page(1)
        except EmptyPage:
            ct = paginate.page(paginate.num_pages)
        return render(request, 'templates/search.html', {'ct': ct, 'query': q, 'city': city, })
    else:
        return HttpResponse('Please submit a search term.')

是否存在阻止表单在模式中显示的错误?

0 个答案:

没有答案