Tuesday, September 20, 2016

Re: Please how do connect a modelform to a CreateView

On Sep 20, 2016 5:16 PM, "Netesy Emmanuel" <netesy1@gmail.com> wrote:
>
> My View.py
>
> class EducationCreate(CreateView):
>     model = Education
>     form_class = EducationForm
>

This is the linkage, so it looks like you already have it in place. Is there a specific issue you are having?

>     def form_valid(self, form):
>         form.instance.student = self.request.user
>         return super(EducationCreate, self).form_valid(form)
>
>     @method_decorator(login_required)
>     def dispatch(self, *args, **kwargs):
>         return super(EducationCreate, self).dispatch(*args, **kwargs)
>

Are you using 1.10? If so, you can take advantage of the new LoginRequiredMixin so that you don't need to decorate the dispatch() method.

https://docs.djangoproject.com/en/1.10/topics/auth/default/#the-loginrequired-mixin

-James

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CA%2Be%2BciUzo6-6nqYE%2Bb8p%3DseeHiqd%2Ba-e%2B9JFKc5-iUavGSWbaA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment