Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django form_valid() and form_invalid() in CreateView not called

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>
like image 722
Ryan Brandt Avatar asked Jul 28 '15 03:07

Ryan Brandt


2 Answers

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

like image 82
Ryan Brandt Avatar answered Sep 17 '22 15:09

Ryan Brandt


Try this

<form action="" method="post">
like image 25
gamer Avatar answered Sep 17 '22 15:09

gamer