Monday, June 25, 2018

Re: how to add radiobuttons to my ModelForm

Can you include your template code? The issue could be in there.

On Sunday, June 24, 2018 at 10:15:50 AM UTC-5, Saloni Kalra wrote:
I wish to add radio buttons fill in each parameter of my model and also to convert it to integer on the backend. But somehow its not changing. Kindly help.
Thanks and regards,
Saloni


Forms.py

class PostForm(forms.ModelForm):

    class Meta:
        fields = ("teacher","subject","param1","param2","param3","param4",
                    "param5","param6","param7","param8","param9","param10",)
        model = models.Post
        choices = (
                    (1,'Unsatisfactory'),
                    (2,'Satisfactory'),
                    (3,'Good'),
                    (4,'Very Good'),
                    (5,'Outstanding')
                    )
        widgets = {
                'param1':forms.TypedChoiceField(choices=choices, widget=forms.RadioSelect, coerce=int),

        }

Models.py

class Post(models.Model):

    user = models.ForeignKey(User, related_name="posts", on_delete=models.CASCADE)
    teacher = models.ForeignKey(Teacher, related_name="posts", on_delete=models.CASCADE)
    subject = models.ForeignKey(Subject, related_name="posts", on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now=True)
    param1 = models.PositiveIntegerField(blank=False, null=False, verbose_name = "The objectives of this course were made clear to me by this teacher.")
    param2 = models.PositiveIntegerField(blank=False, null=False, verbose_name = "The teacher speaks, articulates and explains concepts clearly.")
    param3 = models.PositiveIntegerField(blank=False, null=False, verbose_name = "The teacher adheres to the timings schedule and enforces discipline in the class.")
    param4 = models.PositiveIntegerField(blank=False, null=False, verbose_name = "Interest generated by the teacher.")
    param5 = models.PositiveIntegerField(blank=False, null=False, verbose_name = "The lectures were well structured and focused on the topics.")
    param6 = models.PositiveIntegerField(blank=False, null=False, verbose_name = "Accessibility of the teacher in and out of the class.")
    param7 = models.PositiveIntegerField(blank=False, null=False, verbose_name = "The teacher has fair knowledge on the subject matter.")
    param8 = models.PositiveIntegerField(blank=False, null=False, verbose_name = "Effective use of teaching aids.")
    param9 = models.PositiveIntegerField(blank=False, null=False, verbose_name = "Time spend on lecturing by teacher for course coverage was sufficient and lesson plan was followed.")
    param10 = models.PositiveIntegerField(blank=False, null=False, verbose_name = "The teacher encourage students to raise pertinent questions and answer them.")

    def __str__(self):
        return self.teacher.teacher_name

    def get_absolute_url(self):
        return reverse(
            "posts:single",
            kwargs={
                "username": self.user.username,
                "pk": self.pk
            }
        )

    class Meta:
        ordering = ["-created_at"]
        unique_together = ["user", "teacher", "subject"]

--
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/acb6d1a7-c865-4904-a3b9-1dfda3a918d8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment