Django:根据数据库中的某些值删除表单文件

时间:2019-06-20 05:56:21

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

我想基于数据库中的某些值从表单中删除某些字段。我没有使用此表单将数据插入任何数据库,而是要从此表单数据中创建一个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 等字段将可用表格

1 个答案:

答案 0 :(得分:1)

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