1

I'm rendering a form in a django template using this model:

class Group(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()
    members = models.IntegerField(default=0)
    has_max = models.BooleanField(default=False)
    max_size = models.IntegerField(default=10)
    DAYS = [
        ('Sundays', 'Sundays'),
        ('Mondays', 'Mondays'),
        ('Tuesdays', 'Tuesdays'),
        ('Wednesdays', 'Wednesdays'),
        ('Thursdays', 'Thursdays'),
        ('Fridays', 'Fridays'),
        ('Saturdays', 'Saturdays'),
    ]
    meeting_day = MultiSelectField(
        verbose_name = 'Meeting day(s)',
        choices=DAYS,
        max_choices=6,
        max_length=100
    )
    start_time = TimeField(widget=TimePickerInput)
    end_time = TimeField(widget=TimePickerInput)

    def __str__(self):
        return self.name

And onto this template:

<form method="POST">
    {% csrf_token %}
    {{form.as_p}}
    <button>POST</button>
</form>

I have a couple of issues going forward:

My first issue is that I only want to have max_size be displayed in my template form if the user clicks has_max. Like a conditional, where once the user checks the box, changing has_max to True then they can enter in the max size of the group.

My second issue is that the start_time and end_time to render in my template or on the admin side. I'm not sure how TimePickerInput works, but right now, there are no fields in my template form or admin form that have those time fields.

Also, last thing, if I have both names the exact same (i.e., ('Sundays', 'Sundays'), is it necessary to have both? Or can django figure it out.

Yehuda
  • 27
  • 15

1 Answers1

1

The first Problem as I understand it You want to have this check box and when it will true the Field of max_size will appear this problem needs to solve with javascript

Why ?

Because if You use the Django template you will need to refresh the page when the user changes something and this will overload the server So you need to create an event with js when the use click on the checkbox it will disappear

Also, last thing, if I have both names the exact same (i.e., ('Sundays', 'Sundays'), is it necessary to have both? Or can Django figure it out?

This part ... You need to know that the first value is the value will be stored in the database and the second value is the human-readable value this what will be displayed for the user in the form Read This

You must know you can't put widgets into your model if you want to add it add it to your from in Your admin or form files ... You will override the AdminModel in your admin file and change the formvalue Read This

Ayman
  • 363
  • 2
  • 9