使用 Django,如何在模板中动态设置 ModelForm 字段值?
Posted
技术标签:
【中文标题】使用 Django,如何在模板中动态设置 ModelForm 字段值?【英文标题】:Using Django, how do I set a ModelForm field value dynamically in the template? 【发布时间】:2021-01-27 22:31:43 【问题描述】:我正在尝试利用复选框字段为用户选择的收藏夹创建模型实例。为了使其正常工作,我需要的最后一点是将其中一个表单字段中的默认值设置为等于循环中的值。我会在views.py 文件、表单本身或模板中使用initialize 参数来执行此操作吗?以下是相关代码: 为 html 类标签道歉
models.py
class ReportDirectory(models.Model):
report_name = models.CharField(max_length=300, unique=True, blank=False)
report_desc = models.TextField()
report_type = models.CharField(max_length=300)
report_loc = models.TextField()
slug = models.SlugField(unique=True, max_length=300)
last_update = models.DateTimeField(null=True)
main_tags = models.CharField(max_length=300)
# Renames the item in the admin folder
def __str__(self):
return self.report_name
class Favorite(models.Model):
directory_user = models.ForeignKey(User, on_delete=models.CASCADE)
user_report = models.ForeignKey(ReportDirectory, on_delete=models.CASCADE)
favorited = models.BooleanField(default=False)
def __str__(self):
return str(self.directory_user)+" - "+str(self.user_report)
forms.py
from django import forms
from .models import Favorite
class FavoriteForm(forms.ModelForm):
class Meta:
model = Favorite
fields = '__all__'
widgets =
'favorited': forms.CheckboxInput(attrs=
'type':'checkbox',
'name':'checkbox',
'onchange':'submit()'
)
views.py
from django.shortcuts import render,redirect
from django.views import generic
from .models import ReportDirectory, Favorite
from django.contrib.auth.models import User
from .forms import FavoriteForm
def report_directory(request):
favorite = Favorite.objects.filter(directory_user=request.user.id, favorited=True)
reports = ReportDirectory.objects.exclude(favorite__directory_user=request.user.id, favorite__favorited=True)
favform = FavoriteForm(initial='directory_user':request.user,)
context =
'reports':reports,
'favorite':favorite,
'favform':favform
if request.method == 'POST':
form = FavoriteForm(request.POST)
if form.is_valid():
form.save()
return redirect('/report_directory')
return render(request, 'counter/report_directory.html',context)
html
<thead>
<tr>
<th class="gls-table-expand">Favorite</th>
<th onclick="sortTable(1)" class="gls-table-expand">Report Type</th>
<th onclick="sortTable(2)" class="gls-table-expand">Report Name</th>
<th class="gls-table-expand">Report Description</th>
<th onclick="sortTable(3)" class="gls-table-expand">Last Updated</th>
<th class="gls-table-expand">Main Tags</th>
<th onclick="sortTable(4)" class="gls-table-expand">View Count</th>
</tr>
</thead>
<tbody id="myTable">
% for r in reports.all %
<tr report-name=" r.report_name ">
<td>
<form method="POST">
% csrf_token %
favform.favorited
</form>
</td>
<td><span><img img
% if r.report_type == 'Tableau' %
src=" % static 'images/tableau_icon.svg' %"
% elif r.report_type == 'Excel' %
src=" % static 'images/excel_icon.svg' %"
% elif r.report_type == 'Box' %
src=" % static 'images/excel_icon.svg' %"
% elif r.report_type == 'Internal Report' %
src=" % static 'images/www_icon.svg' %"
% endif %
></span> r.report_type </td>
<td><a href="% url 'report_shell' r.slug %"> r.report_name </a></td>
<td> r.summary </td>
<td><p class="gls-text-meta gls-margin-remove-top"> r.last_update_format </p></td>
<td> r.main_tags </td>
<td>% get_hit_count for r %</td>
</tr>
% endfor %
</tbody>
【问题讨论】:
【参考方案1】:我找到了一个动态迭代模板并完成 ForeignKey(选择)字段的解决方案。我需要将 for 迭代器设置为等于选择选项
<form method="POST" id=" r.report_name ">
% csrf_token %
<p hidden>
favform.directory_user
<select name="user_report" required="" id="id_user_report">
<option value=" r.id " selected></option>
</select>
</p>
favform.favorited
</form>
但是,我觉得我正在使用 Django 表单,而不是使用基础架构...
【讨论】:
以上是关于使用 Django,如何在模板中动态设置 ModelForm 字段值?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用“include”标签在 Django 中动态包含模板
Django基础,Day10 - template 模板引擎与路径设置
如何在 Django 模板中的“带有模板标签”中设置 Django 渲染块值?