Django form.cleaned_data switches values between Form class choice fields

I am trying to filter a model’s objects dynamically depending on what the user selects in a form. The form allows selecting from two dropdown lists. Selecting from either dropdown is optional. If the user does not select anything and clicks Submit, there will be an empty search results page. I believe I didn’t do some things correctly. When I extract data from form.cleaned_data, when field_1 is only selected by the user, its value is assigned to field_2 and field_1 ends up being assigned the string “field_1”. Same for when only field_2 is selected. Why is it switching the value assignment and assigning the string name of each to itself?
Output

self.kwargs = {"field_1": field_2_value, "field_2": "field_2"}
self.kwargs = {"field_1": "field_1", "field_2": field_1_value}

However, when the user selects a value from each dropdown list, the values are properly assigned.

Here is my code:

views.py

class MyDataView(generic.ListView):
    template_name = "my_data/my_data.html"
    context_object_name = "my_data_list"

    def get_context_data(self, **kwargs):
        context = super(MyDataView, self).get_context_data(**kwargs)
        context["form"] = SearchForm
        return context

    def get_queryset(self):
        # Update and create data in the MyData model
        populate_my_data()

        return MyData.objects.all()


class MyDataSearcher(FormView):
    form_class = SearchForm

    def form_valid(self, form):
        field_1 = form.cleaned_data.get("field_1 ", "")
        field_2 = form.cleaned_data.get("field_2 ", "")

        kwargs = {}

        if field_1 :
            kwargs["field_1 "] = field_1 

        if field_2 :
            kwargs["field_2 "] = field_2 
    
        # Redirect to the search results page
        return HttpResponseRedirect(reverse("my_data:search_results_view", kwargs=kwargs))


class SearchResultsView(generic.ListView):
    model = MyData
    template_name = "my_data/search_results.html"

    def get_context_data(self, **kwargs):
        context = super(SearchResultsView, self).get_context_data(**kwargs)

        my_data_list = []
        field_1= self.kwargs.get("field_1", "")
        field_2 = self.kwargs.get("field_2 ", "")
        context["no_result"] = ""

        filters = {}

        if filters:
            try:
                my_data_list = MyData.objects.filter(**filters)
            except (KeyError, MyData.DoesNotExist):
                return render(
                    self.request,
                    "my_data/table_viewer.html",
                    {
                        "error_message": "No data filters selected"
                    }
                )
        else:
            context["no_result"] = "Please select from the lists"

        context["my_data_list"] = my_data_list
        context["field_1"] = field_1
        context["field_2"] = field_2

        # NOTE: For debugging
        context["kwargs"] = self.kwargs

        return context

forms.py

from django import forms

from .populate_data import PopulateChoices


POPULATED_CHOICES = PopulateChoices()


class SearchForm(forms.Form):
    field_1 = forms.ChoiceField(
        choices=POPULATED_CHOICES.field_1_choices,
        label="Field 1",
        required=False
    )

    field_2 = forms.ChoiceField(
        choices=POPULATED_CHOICES.field_2_choices,
        label="Field 2",
        required=False
    )

urls.py

urlpatterns = [
    path("table_viewer/", views.MyDataView.as_view(), name="my_data_view"),
    path("table_viewer/search/", views.MyDataSearcher.as_view(), name="my_data_searcher"),
    path("search_results/<str:field_1>/<str:field_2>/", views.SearchResultsView.as_view(), name="search_results_view"),
    path("search_results/field_1/<str:field_1>/", views.SearchResultsView.as_view(), name="search_results_view"),
    path("search_results/<str:field_2>/field_2/", views.SearchResultsView.as_view(), name="search_results_view"),
    path("search_results/", views.SearchResultsView.as_view(), name="search_results_view")
]

forms.py

<form action={% url "my_data:my_data_searcher" %} method="post">
  {% csrf_token %}
  {{ form }}
  <input name="submitted" type="submit" value="Search">
</form>

I’m expecting that field_1 would only be in the self.kwargs when only field_1 is selected.

self.kwargs = {"field_2": field_2_value}

And when field_2 is only selected, it should only be in self.kwargs.

self.kwargs = {"field_1": field_1_value}

New contributor

Angel L is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật