多表单,单提交按钮

时间:2022-09-25 14:47:33

I'm currently working with django project. I had to filter the data store on the database based on the user input on form (at template) as looked below.

我目前在django项目中工作。我必须基于表单上的用户输入(在模板上)对数据库上的数据存储进行筛选,如下所示。

多表单,单提交按钮

On form user either enter value or leave it blank. So what I have to do is first find the (valid) user input and then fire appropriate query to display data as user input in the form. So final result should be displayed on table at template.

在表单用户要么输入值,要么留空。因此,我要做的是首先找到(有效的)用户输入,然后触发适当的查询,将数据显示为表单中的用户输入。所以最终结果应该显示在模板的表格上。

As I'm new to django, how should I have to pass the data and fire query to represent data at multiple field. As help or link related to these type problem are expected. ( I just able to filter from the database with only one form and had no concept to solve this.)

由于我是django的新手,我应该如何通过数据和fire查询来表示多个字段中的数据。作为与这些类型问题相关的帮助或链接。(我只能用一种形式从数据库中过滤,没有概念来解决这个问题。)

Model of my temp project is as below.

我的临时项目的模型如下。

class exReporter(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.EmailField()
    gender = models.CharField(max_length=1)
    age = models.IntegerField()
    label = models.IntegerField()

1 个解决方案

#1


1  

There are a number of approaches you can take, but here is one solution you can use that involves chaining together the appropriate filters based on the form's posted data:

您可以采用多种方法,但这里有一种解决方案可以使用,即根据表单发布的数据将适当的过滤器链接在一起:

*Note: To conform to Python's naming convention, rename exReporter class to ExReporter.

*注意:为了符合Python的命名约定,将exReporter类重命名为exReporter。

# views.py
def process_ex_reporter_form(request):
    if request.method == "POST":
        # ExReporterForm implementation details not included.
        ex_reporter_form = ExReporterForm(request.POST)

        if ex_reporter_form.is_valid():
            # If form field has no data, cleaned data should be None.
            gender = ex_reporter_form.cleaned_data['gender']
            age_start = ex_reporter_form.cleaned_data['age_start']
            age_end = ex_reporter_form.cleaned_data['age_end']
            aggregation_group = ex_reporter_form.cleaned_data['aggregation_group']
            aggregation_id = ex_reporter_form.cleaned_data['aggregation_id']

            ex_reporters = ExReporter.objects.get_ex_reporters(gender, age_start,
                               age_end, aggregation_group, aggregation_id)

        else:
            # Pass back form for correction.
            pass

    else:
        # Pass new form to user.
        pass

# models.py
class ExReporterManager(models.Manager):
    def get_ex_reporters(self, gender, age_start, age_end, aggregation_group,
                         aggregation_id):

        ex_reporters = super(ExReporterManager, self).get_query_set().all()

        # Even though the filters are being applied in separate statements,
        # database will only be hit once.
        if ex_reporters:
            if gender:
                ex_reporters = ex_reporters.filter(gender=gender)

            if age_start:
                ex_reporters = ex_reporters.filter(age__gt=age_start)

            if age_end:
                ex_reporters = ex_reporters.filter(age__lt=age_end)

            # Apply further filter logic for aggregation types supported.

        return ex_reporters

#1


1  

There are a number of approaches you can take, but here is one solution you can use that involves chaining together the appropriate filters based on the form's posted data:

您可以采用多种方法,但这里有一种解决方案可以使用,即根据表单发布的数据将适当的过滤器链接在一起:

*Note: To conform to Python's naming convention, rename exReporter class to ExReporter.

*注意:为了符合Python的命名约定,将exReporter类重命名为exReporter。

# views.py
def process_ex_reporter_form(request):
    if request.method == "POST":
        # ExReporterForm implementation details not included.
        ex_reporter_form = ExReporterForm(request.POST)

        if ex_reporter_form.is_valid():
            # If form field has no data, cleaned data should be None.
            gender = ex_reporter_form.cleaned_data['gender']
            age_start = ex_reporter_form.cleaned_data['age_start']
            age_end = ex_reporter_form.cleaned_data['age_end']
            aggregation_group = ex_reporter_form.cleaned_data['aggregation_group']
            aggregation_id = ex_reporter_form.cleaned_data['aggregation_id']

            ex_reporters = ExReporter.objects.get_ex_reporters(gender, age_start,
                               age_end, aggregation_group, aggregation_id)

        else:
            # Pass back form for correction.
            pass

    else:
        # Pass new form to user.
        pass

# models.py
class ExReporterManager(models.Manager):
    def get_ex_reporters(self, gender, age_start, age_end, aggregation_group,
                         aggregation_id):

        ex_reporters = super(ExReporterManager, self).get_query_set().all()

        # Even though the filters are being applied in separate statements,
        # database will only be hit once.
        if ex_reporters:
            if gender:
                ex_reporters = ex_reporters.filter(gender=gender)

            if age_start:
                ex_reporters = ex_reporters.filter(age__gt=age_start)

            if age_end:
                ex_reporters = ex_reporters.filter(age__lt=age_end)

            # Apply further filter logic for aggregation types supported.

        return ex_reporters