So I'm new to django and I'm struggling with the documentation for class based views. Not really sure what I'm doing wrong here, but this is the problem:
I fill in the appropriate data on the form, click submit and I'm redirected to the same url plus some get
parameters that correspond to what I submitted in the form. Nothing new is added to the db. I would like to create a new Advertisement
row in the db when I click submit.
I have a ModelForm as follows:
class NewAdForm(ModelForm):
class Meta:
model = Advertisement
exclude = ('campaign',)
def __init__(self, campaign, *args, **kwargs):
super(NewAdForm, self).__init__(*args, **kwargs)
self.campaign = campaign
I also have a FormView:
class AddAdvertView(FormView):
form_class = NewAdForm
template_name = 'advertisers/newad.html'
def get_form_kwargs(self):this
kwargs = super(AddAdvertView, self).get_form_kwargs()
kwargs['campaign'] = get_object_or_404(Campaign, id__exact = self.kwargs['campaign_id'])
return kwargs
def form_valid(self, form):
form.save(commit = True)
return super(AddAdvertView, self).form_valid(form)
And here's the template:
<form action="" method="get">
{{ form.as_p }}
<input type="submit" value="Submit"/>
</form>