Django: Model Form “object has no attribute 'cleaned_data'”

后端 未结 3 900
星月不相逢
星月不相逢 2020-12-22 17:51

I am trying to make a search form for one of my classes. The model of the form is:

from django import forms
from django.forms import CharField, ModelMultiple         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-22 18:08

    I would write the code like this:

    def search_book(request):
        form = SearchForm(request.POST or None)
        if request.method == "POST" and form.is_valid():
            stitle = form.cleaned_data['title']
            sauthor = form.cleaned_data['author']
            scategory = form.cleaned_data['category']
            return HttpResponseRedirect('/thanks/')
        return render_to_response("books/create.html", {
            "form": form,
        }, context_instance=RequestContext(request))
    

    Pretty much like the documentation.

提交回复
热议问题