Django form_valid() and form_invalid() in CreateView not called
Asked Answered
Y

2

8

I am currently using django's CreateView to post data to the database. Unfortunately, the method where I would like to save and perform custom logic, form_valid() is never called. I read on another stack overflow response that form_invalid() might uncover the problem, but I can't get that method to call either. The only method that seems to call is get(), which I overrode and put a print statement in. What am I doing wrong?

the class declaration in view.py

class TeamCreate(CreateView): # Manipulate and use this Method instead of create_team
    model = Team
    # form_class = create_team_form
    fields = ['team_name', 'sport', 'sport_season']
    success_url = '/'

    def get(self, request, *args, **kwargs):
        self.user = request.user
        print 'happening1'
        return super(TeamCreate, self).get(request, *args, **kwargs)


    def form_valid(self, form):
        print 'happening2'
        # form.instance.save()
        # self.user.teams.add(form.instance)
        form.save()
        return super(form_valid, self).form_valid(form)

    def form_invalid(self, form):
        print "form is invalid"
        return http.HttpResponse("form is invalid.. this is just an HttpResponse object")

the relevant template code

{% block content %}
<form action="" method=”POST”>
     {% csrf_token %}
     {{ form|crispy }}
     <input class="btn btn-primary" type="submit" value="Create" />
</form>
Yolandayolande answered 28/7, 2015 at 3:48 Comment(0)
Y
39

Apparently the quotation marks around POST were not quotation marks at all, but sneaky ninja life ruining marks. " versus ”. I'm going to bed.

Yolandayolande answered 28/7, 2015 at 4:1 Comment(2)
what? what you mean?Togoland
Funniest, most-relatable SO post I've ever seen 😂->😭Barocchio
H
5

Try this

<form action="" method="post">
Hughey answered 28/7, 2015 at 11:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.