Django-HTML:如何允许用户添加其他输入字段(确保它们不是必需的)?类似于+按钮 [英] Django-HTML: How can i allow users to add additional input fields (ensuring they are not required)? Something like a + button
问题描述
Django-HTML:如何允许用户添加其他输入字段(确保不需要)?类似于+按钮.我只想显示信息1.并且只有当用户按"+"按钮,这些字段将出现,并允许他们添加info_2和info_3.这可能吗?因为我需要在model.py中提供一个额外的字段:例如 info_4 = ..... info_5
= .......
Django-HTML: How can i allow users to add additional input fields (ensuring they are not required)? 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
=.......
我已经搜索了Stackoverflow,但是找不到与之非常相似的问题和答案.也许有人可以就编码提出建议,因为我相信您的输入对django和html初学者都将非常有益:)
I have searched the Stackoverflow but was unable to find a question and answer very similar to this. Maybe someone can advice on the coding as i believe your input will be very beneficial to both django and html beginners:)
models.py
models.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:
<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
class CreateBlogPostForm(forms.ModelForm):
class Meta:
model = BlogPost
fields = ['chief_title', 'body' , info_1' ]
推荐答案
您可以创建一个名为 Info
的单独的 Model
,并将ForeignKey关系添加到 post
,那么创建后的 post
作者可以添加任意数量的 info
对象.
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.
这篇关于Django-HTML:如何允许用户添加其他输入字段(确保它们不是必需的)?类似于+按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!