Django: Get the form name or id from template

前端 未结 3 1746
星月不相逢
星月不相逢 2021-01-24 12:21

If you have multiple forms

from django import forms

class NameForm(forms.Form):
    your_name = forms.CharField(label=\'Your name\', max_length=100)

class Sec         


        
3条回答
  •  Happy的楠姐
    2021-01-24 13:08

    In your view, you can do form.__class__.__name__ but this doesn't work inside a template because django templates do not recognize variables that begin with __

    The best thing to do in your case would be to simply create an extra context variable that identifies the form name

    id="{{form_name}}_{{field.val}}"
    

提交回复
热议问题