I want to add a subject into a particular id using generic CreatView, i keeping getting errors 'WSGIRequest' object has no attribute 'profile'. Please need help on how to resolve this.
models.py
class Course(models.Model):
profile = models.ForeignKey(Profile, on_delete=models.CASCADE, null=True)
subject = models.CharField(max_length=20)
score = models.CharField(max_length=20)
grade = models.CharField(max_length=20)
def get_absolute_url(self,):
return reverse('student:detail', kwargs={'pk': self.pk})
class Profile(models.Model):
firstName = models.CharField(max_length=30)
middleName = models.CharField(max_length=30)
lastName = models.CharField(max_length=30)
dob = models.DateField(blank=True, null=True)
stud_id = models.CharField(max_length=5000)
gender = models.CharField(max_length=10)
parentsName =models.CharField(max_length=30)
address = models.CharField(max_length=250)
lga = models.CharField(max_length=250)
Religion = models.CharField(max_length=50)
email = models.EmailField(max_length=70, null=True, blank=True, unique=True)
phone = models.CharField(max_length=11, unique=True)
photo = models.FileField()
Comment = models.CharField(max_length=250)
def get_absolute_url(self):
return reverse('student:detail', kwargs={'pk': self.pk})
def __str__(self):
return self.firstName
#Add Subject section
class CourseCreate(CreateView):
model = Course
form_class = CourseForm
template_name = 'student/course_form.html'
def form_valid(self, form):
form.instance.profile = self.request.profile
form.instance.profile = Profile.objects.get(pk=self.kwargs['pk'])
return super().form_valid(form)
forms.py
class CourseForm(forms.ModelForm):
class Meta:
model = Course
fields = ['subject', 'score', 'grade']urls.pyurl(r'^course_form/(?P<pk>[0-9]+)/$', views.CourseCreate.as_view(), name='course_form'),
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/e1b2424e-b966-4105-9c2a-8204dd71726b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment