Django FormPreview: Save form data to database

149 views
Skip to first unread message

jthewriter

unread,
Mar 8, 2017, 8:18:50 PM3/8/17
to Django users

Probably a simple question but having trouble implementing a form preview page using django-formtools. I've configured everything per the docs. I'm stuck on what to add to the done() method to save the data to db.


forms.py

class JobForm(ModelForm):
    class Meta:
        model = Job
        fields = ('title', 'category', 'company', 'website',    'description',)

class JobFormPreview(FormPreview):
    def done(self, request, cleaned_data):
        # add what here to save form data as object?
        return HttpResponseRedirect('/success')


urls.py

...
url(r'^jobs/new/$',
    JobFormPreview(JobForm),
    name='job_form'),
...


Using the default templates. The form and preview both render fine, but obviously data doesn't save on submit. Tried self.form.save() but get an error save() missing 1 required positional argument: 'self'.


I appreciate any guidance.

ludovic coues

unread,
Mar 9, 2017, 6:30:26 AM3/9/17
to django...@googlegroups.com
If you want to create a Job object and save it to database, I highly
recommend the CreateView

https://docs.djangoproject.com/en/1.10/ref/class-based-views/generic-editing/#django.views.generic.edit.CreateView
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/a2a45d4e-d7cf-469b-9721-990488a3450f%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

jthewriter

unread,
Mar 9, 2017, 10:20:12 AM3/9/17
to Django users
Yeah, that's actually what I was doing before, but as far as I can tell formtools prevents that possibility. Do you know of some way to create the desired 'preview page' behavior while using CreateView?

ludovic coues

unread,
Mar 9, 2017, 6:21:13 PM3/9/17
to django...@googlegroups.com
Oh, sorry, I didn't notice the form preview functionality.

CreateView simply call form.save() but it have access to a ModelForm.
I assume you want to create the object and save it in the done function.

I would try that:

def done(self, request, cleaned_data):
job = Job(**cleaned_data)
job.save()
return HttpResponseRedirect(url_for("job:success")
> https://groups.google.com/d/msgid/django-users/3f94a9bd-ad7a-467e-b897-f1410a6f8878%40googlegroups.com.

jthewriter

unread,
Mar 9, 2017, 6:29:31 PM3/9/17
to Django users
Thanks. That's essentially what I ended up doing just with job = Job.objects.create(**cleaned_data)

Appreciate your help!
Reply all
Reply to author
Forward
0 new messages