[英]Django-HTML: How can i allow users to add additional input fields (ensuring they are not required)? Something like a + button
Django-HTML: How can i allow users to add additional input fields (ensuring they are not required)? Django-HTML:我如何允许用户添加额外的输入字段(确保它们不是必需的)? Something like a + button.
类似于 + 按钮的东西。 I only want to display info 1. And only when users press the '+" button, these fields will appear and allow them to add info_2 and info_3. Is this possible? Because do i need to provide like an additional field in the model.py: eg
info_4 =..... info_5
=.......我只想显示信息 1。只有当用户按下“+”按钮时,这些字段才会出现并允许他们添加 info_2 和 info_3。这可能吗?因为我需要在 model 中提供类似的附加字段。 py:例如
info_4 =..... info_5
=.......
I have searched the Stackoverflow but was unable to find a question and answer very similar to this.我搜索了 Stackoverflow,但找不到与此非常相似的问题和答案。 Maybe someone can advice on the coding as i believe your input will be very beneficial to both django and html beginners:)
也许有人可以对编码提出建议,因为我相信您的输入将对 django 和 html 初学者都非常有益:)
models.py模型.py
class BlogPost(models.Model):
title = models.CharField(max_length=50, null=False, blank=False)
body = models.TextField(max_length=5000, null=False, blank=False)
info_1 = models.TextField(max_length=300, null=False, blank=False)
info_2 = models.TextField(max_length=3000, null=False, blank=False)
info_3 = models.TextField(max_length=300, null=True, blank=True)
html: html:
<form class="create-form" method="post" enctype="multipart/form-data">{% csrf_token %}
<!-- chief_title -->
<div class="form-group">
<label for="id_title">Chief Title!</label>
<input class="form-control" type="text" name="chief_title" id="id_title" placeholder="Title" required autofocus>
</div>
<!-- Body -->
<div class="form-group">
<label for="id_body">Full Description</label>
<textarea class="form-control" rows="8" type="text" name="body" id="id_body" placeholder="This is about..." required></textarea>
</div>
<div class="form-group">
<label for="id_Info_1">Info_1</label>
<input class="form-control" rows="10" type="text" name="Info_1" id="id_Info_1" placeholder="Info_1..." required></input>
</div>
forms.py forms.py
class CreateBlogPostForm(forms.ModelForm):
class Meta:
model = BlogPost
fields = ['chief_title', 'body' , info_1' ]
You can create a seprate Model
called Info
and add a ForeignKey relationship to a post
, then the post
author after its creation could add as many info
objects as he would want.您可以创建一个名为
Info
的Model
并将 ForeignKey 关系添加到post
,然后post
作者在创建后可以添加任意数量的info
对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.