在 Django 中创建动态表单
Creating a Dynamic Form in Django
我目前正在从事一个项目,该项目将有一个骗局报告数据库。在网站的报告部分,我有一个表格,但我希望任何人都能够通过单击按钮添加多个配置文件。例如:
Nickname field: xyz
Steam profile: x
[ + ] <- button for adding more profiles, which when pressed would look something like this:
Nickname field: xyz
Steam profile: x
Steam profile 2: y [ Delete ]
[ + ]
我正在研究 FormSets 和 Inline Formsets,但没有发现任何符合这一特定需求的东西,也没有回答有关存储表单结果的问题。
我将如何为此创建表单?
如何将 Steam profile
的多个结果存储到具有 steam_profile = models.CharField
的对象中?
我目前的型号:
class Scammer(models.Model):
#Basic information for display
steam_id_64 = models.CharField(max_length=17, default='00000000000000000')
nickname = models.CharField(max_length=64, default='Nickname')
steam_profile = models.CharField(max_length=512, default='https://www.steamcommunity.com')
description = models.TextField(default='')
proof = models.TextField(default='')
#Date created var for ordering
date_created = models.DateTimeField(default=timezone.now, blank=True)
def __str__(self):
return self.nickname
def get_absolute_url(self):
return reverse('dashboard-home')
我的看法:
class ScammerCreateView(CreateView):
model = Scammer_Unapproved
template_name='dashboard/report.html'
fields = ['nickname', 'steam_id_64', 'steam_profile', 'description', 'proof']
我的模板:
{% block content %}
<div class="report-scammer">
<form method="POST">
{% csrf_token %}
<fieldset>
<legend>Report a scammer</legend>
{{ form|crispy }}
</fieldset>
<div>
<button type="submit">Report</button>
</div>
</form>
</div>
{% endblock content %}
将配置文件作为 内置 Django 用户模型 或使用 link 内置用户模型维护一个单独的模型。现在,可以使用 ManytoMany 关系字段 将诈骗报告表 link 编辑到多个用户帐户。查看下面的官方文档页面,
[https://docs.djangoproject.com/en/3.1/topics/db/examples/many_to_many/][1]
我目前正在从事一个项目,该项目将有一个骗局报告数据库。在网站的报告部分,我有一个表格,但我希望任何人都能够通过单击按钮添加多个配置文件。例如:
Nickname field: xyz
Steam profile: x
[ + ] <- button for adding more profiles, which when pressed would look something like this:
Nickname field: xyz
Steam profile: x
Steam profile 2: y [ Delete ]
[ + ]
我正在研究 FormSets 和 Inline Formsets,但没有发现任何符合这一特定需求的东西,也没有回答有关存储表单结果的问题。
我将如何为此创建表单?
如何将 Steam profile
的多个结果存储到具有 steam_profile = models.CharField
的对象中?
我目前的型号:
class Scammer(models.Model):
#Basic information for display
steam_id_64 = models.CharField(max_length=17, default='00000000000000000')
nickname = models.CharField(max_length=64, default='Nickname')
steam_profile = models.CharField(max_length=512, default='https://www.steamcommunity.com')
description = models.TextField(default='')
proof = models.TextField(default='')
#Date created var for ordering
date_created = models.DateTimeField(default=timezone.now, blank=True)
def __str__(self):
return self.nickname
def get_absolute_url(self):
return reverse('dashboard-home')
我的看法:
class ScammerCreateView(CreateView):
model = Scammer_Unapproved
template_name='dashboard/report.html'
fields = ['nickname', 'steam_id_64', 'steam_profile', 'description', 'proof']
我的模板:
{% block content %}
<div class="report-scammer">
<form method="POST">
{% csrf_token %}
<fieldset>
<legend>Report a scammer</legend>
{{ form|crispy }}
</fieldset>
<div>
<button type="submit">Report</button>
</div>
</form>
</div>
{% endblock content %}
将配置文件作为 内置 Django 用户模型 或使用 link 内置用户模型维护一个单独的模型。现在,可以使用 ManytoMany 关系字段 将诈骗报告表 link 编辑到多个用户帐户。查看下面的官方文档页面,
[https://docs.djangoproject.com/en/3.1/topics/db/examples/many_to_many/][1]