how to add radiobuttons to my ModelForm

27 views
Skip to first unread message

Saloni Kalra

unread,
Jun 24, 2018, 11:15:50 AM6/24/18
to django...@googlegroups.com
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"]

alex eckert

unread,
Jun 25, 2018, 2:47:07 PM6/25/18
to Django users
Can you include your template code? The issue could be in there.

Saloni Kalra

unread,
Jun 26, 2018, 5:13:01 AM6/26/18
to Django users
template:

{% extends "posts/post_base.html" %}

{% load bootstrap3 %}

{% block post_content %}
<h4>{% if not form.instance.pk %}
Create Post
{% else %}
Update Post
{% endif %}</h4>
<form method="POST" id="postForm">
{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" value="Post" class="btn btn-primary btn-large">
</form>
{% endblock %}
Reply all
Reply to author
Forward
0 new messages