我创建了一个表单窗口小部件,它自动在窗体顶部添加一个添加新记录部分,我不想在那里.谁能告诉我如何禁用它?我只想显示变量而不是添加表单.
forms.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__"
admin.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 %}{% endif %} {% endblock %}
加载的页面: