【问题标题】:Django: Remove form fileds based on some values in the databaseDjango:根据数据库中的某些值删除表单字段
【发布时间】:2019-11-02 21:47:40
【问题描述】:

我想根据数据库中的某些值从表单中删除某些字段。我没有使用此表单将数据插入任何数据库,我将使用此表单数据制作一个 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')

现在我要做的是使用 URL 中 "id" 的值从数据库中找到 es_event 的特定实例。

然后,如果该实例的属性 registrant_name,registrant_age 等为 True,则字段 Applicant_Name、Applicant_age 等将在表格

【问题讨论】:

    标签: django django-models django-forms django-urls


    【解决方案1】:

    您可以为此使用 AJAX。我认为this 是一个与您类似的示例,只是不是检查用户是否存在,而是检查您的实例是否具有所需的属性(registrant_name、registrant_age)。当您收到 JSON 响应时,您可以使用 Javascript 显示/隐藏字段。

    【讨论】:

      猜你喜欢
      • 2016-11-27
      • 2010-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-22
      • 2018-03-03
      • 2011-08-30
      相关资源
      最近更新 更多