Hi everyone, i’m new here.
So, i’m working with Django since last year, and i’m still learning working with it. It’s a great framework, but there’s a lot to figure out.
What i’am trying to do, is a pagined form.
Right now, my form is working, the pagination is working, even the filter form to reduce overall choices works.
But i struggle to have a consistent overall form.
If i check options whithin the first page, i loose those options when changing page.
Wich means, i’m not abble to navigate efficiently throught all pages, and i will not able to validate the whole options trought all pages too.
I’m working on a pretty big project, so my basecode is splitted in many classes and files.
I share, what i suppose to be the more obvious to work with:
First, the form code:
class OrderProductForm(forms.Form):
ORDER = []
PRODUCT_CHOICES = []
order = forms.ChoiceField(
widget=forms.Select(attrs={"class": "form_select"}),
label="Commande",
required=True,
)
product = forms.MultipleChoiceField(
widget=forms.CheckboxSelectMultiple(attrs={"class": "form_checkbox"}),
label="Produits disponibles",
required=True,
)
The view code:
class AvailableProducts(Base, CreationView):
search_form_class = ProductSearchForm
product_collection = None
order = None
ITEMS_PER_PAGE = 23
def __init__(self):
super().__init__()
self.product_data = ProductData()
def _get_template_path(self):
template_path = self.template_path_creator.create_available_products_path()
return template_path
def add_order_uuid_to_context_dictionary(self, order):
self._update_context_dictionary({"order_uuid": order.id})
def get_order(self, order_id):
order = Order.objects.filter(id=order_id)
return order
def get_available_products(self):
self.product_collection = self.product_data.get_product_queryset()
def add_product_collection_to_context_dictionary(self, product_collection):
self._update_context_dictionary({"product_collection": product_collection})
def make_product_dictionary_from_product_collection(self, product_collection):
product_dictionary = {}
for product in product_collection:
product_dictionary[product.id] = {
"chip_number": product.chip_number,
}
return product_dictionary
def add_product_dictionary_from_product_collection_to_context_dictionary(
self, product_collection
):
product_dictionary = self.make_product_dictionary_from_product_collection(
product_collection
)
self._update_context_dictionary({"product_dictionary": product_dictionary})
def apply_product_choices(self, form, product_collection):
product_choices = [(product.id, str(product.birth_number)) for product in product_collection]
form.fields["product"].choices = product_choices
return form
def apply_order(self, form, order):
order_choices = []
if order:
for ord in order:
order_choices.append((ord.id, str(ord)))
self.add_order_uuid_to_context_dictionary(ord)
form.fields["order"].choices = order_choices
form.fields["order"].initial = order
return form
def set_configuration_form(self, request_post=None):
form = OrderProductForm(request_post)
page_obj = self.handle_pagination(
collection=self.product_collection, collection_name="product_collection"
)
form = self.apply_product_choices(form, product_collection=page_obj)
form = self.apply_order(form, self.order)
self.add_product_collection_to_context_dictionary(product_collection=page_obj)
self.add_product_dictionary_from_product_collection_to_context_dictionary(
product_collection=page_obj
)
return form
def set_configuration_form_GET(self, *args, **kwargs):
return self.set_configuration_form()
def set_configuration_form_POST(self, request_post, *args, **kwargs):
return self.set_configuration_form(request_post=request_post)
def handle_request_parameters(self, request_parameters):
result = None
parameters = self._decode_request_parameters(request_parameters)
self.order = self.get_order(parameters["order_id"])
if self.request.method == "GET":
result = self.handle_get()
else:
result = self.handle_post()
return result
def apply_filters(self, search_form):
if search_form.is_valid():
self.product_data.apply_chip_number_filter(
search_form.cleaned_data["chip_number"]
)
self.product_data.apply_status_filter(search_form.cleaned_data["status"])
self.product_data.apply_exit_date_none_filter()
self.product_data.apply_order_none_filter()
def get(self, request, order_parameters=None, *args, **kwargs):
self.search_form = self.search_form_class(request.GET)
self._update_context_dictionary({"search_form": self.search_form})
self.apply_filters(self.search_form)
self.get_available_products()
request_parameters = order_parameters
return super().get(request, request_parameters, *args, **kwargs)
def _insert_model_instance_form_to_database(self):
product_instance = None
self.form = self.set_configuration_form_POST(self.request.POST)
if self.form.is_valid():
selected_order = self.form.cleaned_data["order"]
order_instance = Order.objects.get(pk=selected_order)
selected_product_ids = self.form.cleaned_data["product"]
selected_products = Product.objects.filter(id__in=selected_product_ids)
for product_instance in selected_products:
product_instance.order = order_instance
product_instance.save(
user=self.request.user, comment="Ajouté à la commande"
)
return product_instance
def post(self, request, order_parameters=None, *args, **kwargs):
self.search_form = self.search_form_class(request.GET)
self._update_context_dictionary({"search_form": self.search_form})
self.apply_filters(self.search_form)
self.get_available_products()
request_parameters = order_parameters
return super().post(request, request_parameters, *args, **kwargs)
Template code:
<div class="container"> {% create_order_parameters order_uuid as order_parameters %} {% include "product_production/order/sidebar.html" %} <div class="main-content"> <div class="subsection"> {% include "base/pagination.html" with collection=product_collection %}
<form method="post">
{% csrf_token %}
<div class="subsection">
{{ model_form.order }}
</div>
<div class="subsection">
<table class="product_table">
<thead class="product_thead">
<tr>
<th class="col_select">Sélection</th>
<th class="col_chip_number">n° Puce / Nom</th>
</tr>
</thead>
<tbody>
{% for product in model_form.product %}
<tr>
{% get_product_details product.data.value product_dictionary as product_details %}
<td class="col_select">{{ product.tag }}</td>
<td class="col_chip_number">{% get_product_detail "chip_number" product_details %}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% if model_form.product.errors %}
<div class="error">
{{ model_form.product.errors }}
</div>
{% endif %}
</div>
{% include "base/creation_submit.html" %}
</form>
{% include "base/pagination.html" with collection=product_collection %}
</div>
</div>
Thank you!