8
votes

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>
2

2 Answers

33
votes

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

5
votes

Try this

<form action="" method="post">