Django:根据数据库中的某些值删除表单字段
Django: Remove form fileds based on some values in the database
我想根据数据库中的某些值从表单中删除某些字段。我没有使用此表单将数据插入任何数据库,我将使用此表单数据制作一个 csv 文件。此表格也与任何模型无关。
forms.py
class Registration_form(forms.Form):
Applicant_Name = forms.CharField(label='Your name', max_length=100)
Applicant_age = forms.IntegerField(label ='Age of Applicant')
Applicant_email =forms.EmailField(max_length=50)
Applicant_phone = forms.CharField(max_length=10)
views.py
class Registration_View(FormView):
template_name = 'EVENTAPP/Application.html'
form_class = Registration_form
success_url = '/'
def form_valid(self, form):
Applicant_Name = form.cleaned_data['Applicant_Name'],
Applicant_age=form.cleaned_data['Applicant_age'],
Applicant_email=form.cleaned_data['Applicant_email']
Applicant_phone=form.cleaned_data['Applicant_phone']
# do some operations if form data valid
return super().form_valid(form)
models.py
class es_event(models.Model):
ev_name = models.CharField(max_length=100,verbose_name="Event Name")
ev_date = models.DateField(auto_now=False, verbose_name="Date")
ev_description = models.TextField(null=True, verbose_name="Description")
registrant_name = models.BooleanField(default=True )
registrant_age = models.BooleanField(default=False)
registrant_phone = models.BooleanField(default=False)
registrant_email = models.BooleanField(default=False)
registrant_institution = models.BooleanField(default=False)
name = models.CharField(max_length=100,null=True)
reg_open = True
slug = models.SlugField(max_length=250)
def save(self, *args, **kwargs):
self.slug = slugify(self.ev_name)
return super(es_event, self).save(*args, **kwargs)
def get_absolute_url(self):
return reverse('event_detail', kwargs={'id': self.id, 'slug': self.slug })
urls.py
url(r'^events/register(?P<id>\d+)(?:/(?P<slug>[\w\d-]+))?/$', views.Registration_View.as_view(), name='event_application')
现在我要做的是使用 "id" 的值从数据库中找到 es_event 的特定实例 在 URL.
然后如果该实例具有 registrant_name、registrant_age 等属性 True 那么字段Applicant_Name、Applicant_age 等将在表格
上提供
您可以使用 AJAX。我认为 this 是一个类似于您的示例,只是您不检查用户是否存在,而是检查您的实例是否具有所需的属性(registrant_name、registrant_age)。当您收到 JSON 响应时,您 show/hide 字段 Javascript.
我想根据数据库中的某些值从表单中删除某些字段。我没有使用此表单将数据插入任何数据库,我将使用此表单数据制作一个 csv 文件。此表格也与任何模型无关。
forms.py
class Registration_form(forms.Form):
Applicant_Name = forms.CharField(label='Your name', max_length=100)
Applicant_age = forms.IntegerField(label ='Age of Applicant')
Applicant_email =forms.EmailField(max_length=50)
Applicant_phone = forms.CharField(max_length=10)
views.py
class Registration_View(FormView):
template_name = 'EVENTAPP/Application.html'
form_class = Registration_form
success_url = '/'
def form_valid(self, form):
Applicant_Name = form.cleaned_data['Applicant_Name'],
Applicant_age=form.cleaned_data['Applicant_age'],
Applicant_email=form.cleaned_data['Applicant_email']
Applicant_phone=form.cleaned_data['Applicant_phone']
# do some operations if form data valid
return super().form_valid(form)
models.py
class es_event(models.Model):
ev_name = models.CharField(max_length=100,verbose_name="Event Name")
ev_date = models.DateField(auto_now=False, verbose_name="Date")
ev_description = models.TextField(null=True, verbose_name="Description")
registrant_name = models.BooleanField(default=True )
registrant_age = models.BooleanField(default=False)
registrant_phone = models.BooleanField(default=False)
registrant_email = models.BooleanField(default=False)
registrant_institution = models.BooleanField(default=False)
name = models.CharField(max_length=100,null=True)
reg_open = True
slug = models.SlugField(max_length=250)
def save(self, *args, **kwargs):
self.slug = slugify(self.ev_name)
return super(es_event, self).save(*args, **kwargs)
def get_absolute_url(self):
return reverse('event_detail', kwargs={'id': self.id, 'slug': self.slug })
urls.py
url(r'^events/register(?P<id>\d+)(?:/(?P<slug>[\w\d-]+))?/$', views.Registration_View.as_view(), name='event_application')
现在我要做的是使用 "id" 的值从数据库中找到 es_event 的特定实例 在 URL.
然后如果该实例具有 registrant_name、registrant_age 等属性 True 那么字段Applicant_Name、Applicant_age 等将在表格
上提供您可以使用 AJAX。我认为 this 是一个类似于您的示例,只是您不检查用户是否存在,而是检查您的实例是否具有所需的属性(registrant_name、registrant_age)。当您收到 JSON 响应时,您 show/hide 字段 Javascript.