TJB
TJB

Reputation: 3856

Form bound, but has nothing in cleaned_data

I feel like this may be something simple that I am missing, however since the post data is coming from an AJAX call in my angular javascript I couldn't be too sure. In a nutshell, I get data from an AJAX call in request.body, which I have a function to handle it and turn it into a query dictionary to bind to my form. It has worked up until this point. The form is valid according to form.is_valid, and I can see the data is being posted, but it doesn't have anything in it's cleaned_data attribute.

def requestPost(request):
    querystring = urllib.urlencode(ast.literal_eval(request.body))
    postdata = QueryDict(query_string=querystring)

    return postdata

def send(request, thread_id):
    # """Send a message to the thread."""

    account = Account.objects.get(email=request.user.username)
    thread = Thread.objects.get(id=thread_id)

    if request.method == "POST":
        x = requestPost(request)
        form = NewChat(requestPost(request))

        if form.is_valid():
            cleaned_data = form.cleaned_data
            threadchat = ThreadChat.objects.create(text=cleaned_data['message'], account=account, thread=thread)

            broadcast(threadchat.id)

            context = {"threadchat": threadchat}
            return composeJsonResponse(200, "", context)

enter image description here

class NewChat(forms.Form):
    message = forms.Textarea()

Upvotes: 0

Views: 104

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599926

forms.Textarea is not a field, it is a widget. Your form has no actual fields in it, so cleaned_data is empty.

You should use forms.CharField in your form; if you need it to display as a textarea you can pass that as the widget argument:

class NewChat(forms.Form):
    message = forms.CharField(widget=forms.Textarea())

Upvotes: 1

Related Questions