Django - Remove add new record from form widget>

I created a form widget that automatically adds a new record section at the top of the form, which I don’t want to be there. can someone tell me how to disable this? I just want to display the variables, not the add form.

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 %} <form method="POST" class="post-form"> {% csrf_token %} {{ include_form.as_p }} </form> {% endif %} {% endblock %} 
Downloadable Page

: sample problem

+5
source share
2 answers

I managed to find out. I added an include data parameter and sent it to the template according to the following:

 def change_view(self, request, object_id, form_url='', extra_context=None): from sites.models import ConfigVariables config_variables = ConfigVariables.objects.all() extra_context = extra_context or {} extra_context['include_data'] = config_variables extra_context['include_template'] = 'admin/config_variables.html' #extra_context['include_form'] = VariableForm return super(ConfigTemplateAdmin, self).change_view( request, object_id, form_url, extra_context=extra_context, ) 

then in the template

 {% for d in include_data %} 
0
source

If I understand your intentions in your template, you want to include it in your template or form. If so:

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

Another possibility is that you do not want extra_context['include_form'] = VariableForm in your ConfigTemplateAdmin class, and you can create another view (or method in its current form) to add new variables to your form!

+2
source

Source: https://habr.com/ru/post/1265822/


All Articles