简体   繁体   English

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: 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.您可以创建一个名为InfoModel并将 ForeignKey 关系添加到post ,然后post作者在创建后可以添加任意数量的info对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 添加按钮添加其他输入字段并为其分配新的ID /名称HTML / PHP / JS - Add button to add additional input fields and assign them new id/name HTML/PHP/JS 我如何创建HTML5像单选按钮所需的警报? - How can i create HTML5 like required alert for radio button? 如何将用户输入相乘以创建其他字段? - How can I multiply user input to create additional fields? 如何将所需的输入字段放入我的 Js 代码中 - How I can put the required input fields in my Js code 我怎么知道用户是否发布了带有“分享/赞”按钮的内容? - How can I know if user posted something with share/like button? 如何允许用户选择远程站点的HTML元素? - How can I allow users to select HTML elements of a remote site? 我怎样才能在所需的输入上有类似 XNOR 的行为? - How can I have a XNOR-like behaviour on required input? HTML 中的按钮 onclick 功能不断将表单输入字段重置为默认值。 我怎样才能避免这种情况? - Button onclick function in HTML keeps resetting the form input fields to default values. How can I avoid this? 如何在 vue js html 中添加多个图像以及其他输入字段? - How can I add multiple images along with other input fields in vue js html? javascript将html标记添加到所有必填字段 - javascript add html tag to all input required fields
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM