2017-03-23 4 views
5

私はそこにいたくないフォームの上部に自動的に新しいレコードセクションを追加するフォームウィジェットを作成しました。誰かがこれを無効にする方法を教えてもらえますか?私はちょうど追加フォームではなく変数を表示したい。Django - フォームウィジェットから新しいレコードを削除する>

ロードされ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 %} 

ページ: sample of issue

+1

ではなく、あなたのフォーム(' VariableForm')

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, ) 

:以下のとおり、私はデータを含めるオプションを追加し、テンプレートにそれを通じ送ら"__all __" ')は明らかに' {{form.as_p}} 'で、モデルのすべてのフィールドの入力フィールドを表示します。 – dirkgroten

+0

私はあなたを正しく理解していれば、テーブルにデータを表示し、テーブルに表示されているデータの新しいエントリを追加できる隠し(?)フォームも必要です。あれは正しいですか? –

答えて

0

これを把握するために管理しました。 ( `フィールド=すべてのフィールドが含まれ、テンプレート

{% for d in include_data %} 
2

テンプレートの意図を理解している場合は、テンプレートまたはをフォームに含める必要があります。その場合は:

{% 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にしたくないと、あなたのフォームで新しい変数を追加するための(現在のビュー内または方法)別のビューを作成することができるということです!

+0

私は今私が望んでいることを知っていると思う、ちょうど私のdev envに問題があるので、テストすることはできません。 – AlexW

+0

すぐにあなたの問題が解決することを願っています。 –

関連する問題