如果Django模板中的特定字段语句
我使用此模型在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_time
和end_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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我理解的第一个问题,您想拥有此复选框,并且何时将其
max_size
的字段出现,将出现此问题以解决使用javascript
为什么?,
因为如果您使用
django Template
,则需要在用户更改某些内容时刷新页面,这将超载服务器,因此您需要使用js
使用时,单击
复选框
它将消失吗?这部分...您需要知道第一个值是该值将存储在数据库中,第二个值是人类可读的值,该值将以阅读此
如果您想将其添加到您的<<代码> 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 withjavascript
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 withjs
when the use click on thecheckbox
it will disappearThis 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
orform
files ... You will override theAdminModel
in youradmin
file and change theform
value Read This