如果Django模板中的特定字段语句

发布于 2025-02-11 13:28:09 字数 1405 浏览 5 评论 0原文

我使用此模型在Django模板中渲染表单:

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

在此模板上:

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

我有几个问题:

我的第一个问题是我只想在我的模板表格如果用户单击HAS_MAX。就像有条件的一样,一旦用户选中框,将has_max更改为true,他们就可以在组的最大大小中输入。

我的第二个问题是start_timeend_time在我的模板或管理方面渲染。我不确定timePickerInput有效,但是现在,我的模板表单或管理表单中没有字段的字段都没有这些时间字段。

另外,最后一件事,如果我的名字完全相同(即,('Sundays','Sundays'),是否有必要同时拥有?还是Django可以弄清楚。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美羊羊 2025-02-18 13:28:09

我理解的第一个问题,您想拥有此复选框,并且何时将其max_size的字段出现,将出现此问题以解决使用javascript

为什么?

因为如果您使用django Template,则需要在用户更改某些内容时刷新页面,这将超载服务器,因此您需要使用js使用时,单击复选框它将消失

另外,最后一件事,如果我两个名字完全相同(即(“星期日”,'Sundays'),是否有必要同时拥有?或者Django可以弄清楚吗?

吗?这部分...您需要知道第一个值是该值将存储在数据库中,第二个值是人类可读的值,该值将以阅读此

如果您想将其添加到您的<<代码> admin 或表单文件...您将在adminmodel中覆盖admin> admin文件,然后更改表单阅读此

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文