Tuesday, September 20, 2016

Please how do connect a modelform to a CreateView

My View.py

class EducationCreate(CreateView):
    model = Education
    form_class = EducationForm

    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)

url.py
url(r'^settings/education/add/$', views.EducationCreate.as_view(), name='education-add')

model.py
class Education(models.Model):
    student = models.ForeignKey(settings.AUTH_USER_MODEL)
    school = models.CharField(max_length=250)
    started = models.DateField()
    finished = models.DateField()
    degree = models.CharField(max_length=250)

Forms.py
class EducationForm(forms.ModelForm):
    school = forms.CharField(label="School Name",max_length=250,help_text="Please enter the school name.")
    started = forms.DateField(label="Started School",)
    finished = forms.DateField(label="Finished School",)
    degree = forms.CharField(label="Degree Earned",max_length=250, help_text="Please enter the degree earned.")
   
    class Meta:
        model = Education
        fields = ('student','school','started','finished','degree')

--
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/20aa27c3-b75d-47cb-b524-6d6197a963bd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment