来自数据库的 Django Eval
Django Eval from Database
我和我的小组正在尝试使用 Django 开发 "very-dynamic" 网络应用程序。
我们为用户提供了一个 "configurations" 模板,每个人都可以配置 his/her 自己的 space。
每个配置选项都应该从数据库中读取。
查看:
def config(request):
if 'logged' not in request.session:
return redirect(login)
apps_list = App.objects.all()
varS = varsesion(request)
context = Configurations.objects.all()
for row in context:
row.form = eval(row.form) #Not sure at all if this is ok
print (row.form)
context.App = 'config'
return render(request, "index.html", {'context': context, 'apps_list ': apps_list , 'varS': varS,
'context.App': context.App})
在我们的数据库中,我们有这样的 Configurations
模型 table:
+----+-----+-----+-----+---------+---------------+
| id |user |Title|Body |OptionBtn| form |
+----+-----+-----+-----+---------+---------------+
| N |userA| X | X | X | DataConfig() |
+----+-----+-----+-----+---------+---------------+
| N2 |userA| X | X | X | ColorsConfig()|
+----+-----+-----+-----+---------+---------------+
| N3 |userA| X | X | X |ButtonsConfig()|
+----+-----+-----+-----+---------+---------------+
以及我将跳过的许多其他专栏...
当然,数据库 form
字段 中的每个 Form
值都以各自的名称存在于 forms.py 中。
当我尝试在我们的模板中迭代这些表单字段时出现问题(数据库中的每一列都正确显示,如按钮、标题、文本等)
这是模板(我在示例中跳过了一些属性 table):
<div class="breadcrumb">
<li class="breadcrumb-item">
<a href="/config.html"><h1 style="color:#871830">Configurations panel</h1></a>
</li>
</div>
{% for configuration in context %}
<div style="" id="panel{{ configuration.codConfi }}" class="breadcrumb form-inline">
<div class="form-group col-12" style="color:#228718"><h3>{{ configuration.title}}</h3></div>
<div class="form-group col-3 m-1">
<label class="form-group">{{ configuration.body }}</label>
</div>
<button id="btn{{ configuration.codConfi }}" type="submit" class="btn btn-info ml-auto mr-0 mr-md-3 my-2 my-md-4">{{ configuration.OptionBtn }}</button>
</div>
这一切都显示得恰到好处,完美无缺。但是当涉及到 form
数据库列时...
<!-- _______________________Forms iterations__________________________ -->
<form style="display:none" id="frm{{ configuration.codConfi }}" class="breadcrumb form-inline" action="/Config/{{ configuration.codConfi }}" method="POST" enctype="application/x-www-form-urlencoded">{% csrf_token %}
<div class="form-group col-12" style="color:#228718"><h3>Configure {{ configuration.Title }}</h3></div>
{% for field in configuration.form %}
<div class="form-group col-3 m-1">
<label class="form-group">{{ field.label }}: </label>
{{ field }}
</div>
{% endfor %}
<button type="submit" class="btn btn-success ml-auto mr-0 mr-md-3 my-2 my-md-4">Apply changes</button>
</form>
{% endfor %}
(如果您看到一些属性在我的示例中没有显示 table 那只是因为我没有输入所有这些)。
它没有正确显示实际表单,而是将 form
数据库列的值显示为字符串。例如,对于 form
列 (DataConfig()
) 中的第一个值,它在迭代中将每个字母显示为字符串(首先显示 "D",然后显示 "a",然后"t",等等,直到最后一个“)”)。
我如何告诉 Django 它不是一个字符串值而是一个变量?
在您的 Django 模型 Configuration
上,您可以添加一个 属性 来获取和实例化实际形式 class。
例如:
class Configuration(models.Model):
...
def get_form(self):
# 'forms_module' is the Python module (file) that holds the form classes
# 'self.form' is a string that holds the name of the form class
form_class = getattr(forms_module, self.form)
# create an (un-bound) instance of the form
return form_class()
然后,在您的模板中(假设 configuration
是模型 Configuration
的一个实例)您可以更改此行:
{% for field in configuration.form %}
进入这个
{% for field in configuration.get_form %}
注意:要使其正常工作,您需要在数据库字段中存储不带括号的形式 class 名称(或在调用 getattr(forms_module, self.form)
之前删除括号)。
如果您需要更具体的内容,您需要为您的问题添加更多信息。
我和我的小组正在尝试使用 Django 开发 "very-dynamic" 网络应用程序。 我们为用户提供了一个 "configurations" 模板,每个人都可以配置 his/her 自己的 space。 每个配置选项都应该从数据库中读取。
查看:
def config(request):
if 'logged' not in request.session:
return redirect(login)
apps_list = App.objects.all()
varS = varsesion(request)
context = Configurations.objects.all()
for row in context:
row.form = eval(row.form) #Not sure at all if this is ok
print (row.form)
context.App = 'config'
return render(request, "index.html", {'context': context, 'apps_list ': apps_list , 'varS': varS,
'context.App': context.App})
在我们的数据库中,我们有这样的 Configurations
模型 table:
+----+-----+-----+-----+---------+---------------+
| id |user |Title|Body |OptionBtn| form |
+----+-----+-----+-----+---------+---------------+
| N |userA| X | X | X | DataConfig() |
+----+-----+-----+-----+---------+---------------+
| N2 |userA| X | X | X | ColorsConfig()|
+----+-----+-----+-----+---------+---------------+
| N3 |userA| X | X | X |ButtonsConfig()|
+----+-----+-----+-----+---------+---------------+
以及我将跳过的许多其他专栏...
当然,数据库 form
字段 中的每个 Form
值都以各自的名称存在于 forms.py 中。
当我尝试在我们的模板中迭代这些表单字段时出现问题(数据库中的每一列都正确显示,如按钮、标题、文本等)
这是模板(我在示例中跳过了一些属性 table):
<div class="breadcrumb">
<li class="breadcrumb-item">
<a href="/config.html"><h1 style="color:#871830">Configurations panel</h1></a>
</li>
</div>
{% for configuration in context %}
<div style="" id="panel{{ configuration.codConfi }}" class="breadcrumb form-inline">
<div class="form-group col-12" style="color:#228718"><h3>{{ configuration.title}}</h3></div>
<div class="form-group col-3 m-1">
<label class="form-group">{{ configuration.body }}</label>
</div>
<button id="btn{{ configuration.codConfi }}" type="submit" class="btn btn-info ml-auto mr-0 mr-md-3 my-2 my-md-4">{{ configuration.OptionBtn }}</button>
</div>
这一切都显示得恰到好处,完美无缺。但是当涉及到 form
数据库列时...
<!-- _______________________Forms iterations__________________________ -->
<form style="display:none" id="frm{{ configuration.codConfi }}" class="breadcrumb form-inline" action="/Config/{{ configuration.codConfi }}" method="POST" enctype="application/x-www-form-urlencoded">{% csrf_token %}
<div class="form-group col-12" style="color:#228718"><h3>Configure {{ configuration.Title }}</h3></div>
{% for field in configuration.form %}
<div class="form-group col-3 m-1">
<label class="form-group">{{ field.label }}: </label>
{{ field }}
</div>
{% endfor %}
<button type="submit" class="btn btn-success ml-auto mr-0 mr-md-3 my-2 my-md-4">Apply changes</button>
</form>
{% endfor %}
(如果您看到一些属性在我的示例中没有显示 table 那只是因为我没有输入所有这些)。
它没有正确显示实际表单,而是将 form
数据库列的值显示为字符串。例如,对于 form
列 (DataConfig()
) 中的第一个值,它在迭代中将每个字母显示为字符串(首先显示 "D",然后显示 "a",然后"t",等等,直到最后一个“)”)。
我如何告诉 Django 它不是一个字符串值而是一个变量?
在您的 Django 模型 Configuration
上,您可以添加一个 属性 来获取和实例化实际形式 class。
例如:
class Configuration(models.Model):
...
def get_form(self):
# 'forms_module' is the Python module (file) that holds the form classes
# 'self.form' is a string that holds the name of the form class
form_class = getattr(forms_module, self.form)
# create an (un-bound) instance of the form
return form_class()
然后,在您的模板中(假设 configuration
是模型 Configuration
的一个实例)您可以更改此行:
{% for field in configuration.form %}
进入这个
{% for field in configuration.get_form %}
注意:要使其正常工作,您需要在数据库字段中存储不带括号的形式 class 名称(或在调用 getattr(forms_module, self.form)
之前删除括号)。
如果您需要更具体的内容,您需要为您的问题添加更多信息。