gpt4 book ai didi

python - Django - 从表单小部件中删除添加新记录>

转载 作者:太空狗 更新时间:2023-10-30 01:26:42 25 4
gpt4 key购买 nike

我已经创建了一个表单小部件,它会自动在表单顶部添加一个添加新记录部分,我不想在那里。有人可以告诉我如何禁用它吗?我只想显示变量而不是添加表单。

表单.py

class TemplateVariablesWidget(forms.Widget):
template_name = 'sites/config_variables.html'

def render(self, name, value, attrs=None):
sc_vars = ConfigVariables.objects.filter(type='Showroom')
wc_vars = ConfigVariables.objects.filter(type='Major Site')
context = {
'SConfigVariables' : sc_vars,
'WConfigVariables' : wc_vars,
}
return mark_safe(render_to_string(self.template_name, context))


class VariableForm(forms.ModelForm):
variables = forms.CharField(widget=TemplateVariablesWidget, required=False)

class Meta:
model = ConfigVariables
fields = "__all__"

管理员.py

class ConfigTemplateAdmin(admin.ModelAdmin):  
list_display = ('device_name', 'date_modified')
def change_view(self, request, object_id, form_url='', extra_context=None):
extra_context = extra_context or {}
#extra_context['include_template'] = '/path/to/template.html'
extra_context['include_form'] = VariableForm
return super(ConfigTemplateAdmin, self).change_view(
request, object_id, form_url, extra_context=extra_context,
)

change_view.html

{% block extra_content %}
{% if include_template %}
{% include include_template %}
{% endif %}
{% if include_form %}
<form method="POST" class="post-form">
{% csrf_token %}
{{ include_form.as_p }}
</form>
{% endif %}
{% endblock %}

加载的页面: sample of issue

最佳答案

如果我理解您在模板中的意图,您希望包含您的模板包含表单。如果是这样的话:

{% if include_template %}
{% include include_template %}
{% elif include_form %}
<form method="POST" class="post-form">
{% csrf_token %}
{{ include_form.as_p }}
</form>
{% endif %}

另一种可能性是,您不想在 ConfigTemplateAdmin 类中使用 extra_context['include_form'] = VariableForm,您可以创建不同的 View (或方法您当前的 View )为您的表单添加新变量!

关于python - Django - 从表单小部件中删除添加新记录>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42976139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com