Best way to update multiple templates based on form submission?

I'm working on a project that does several calculations based on user input, and I am now trying to create a new form in my variables_input template where the user can retrieve the values used in prior runs, and this works on the variables_input template resulting with the values being displayed on the page. However when the user goes to another page the values displayed are the most recently run calculations. How do I make it so when the user chooses a past run that they don't have to run the calculations but just get the values from the appropriate models to be displayed on the appropriate page? I was wondering if signals could achieve the desired result or if a simpler solution is possible. Below is an example of my project's code.

variables_input.html

{% block content %}
<form action="" method="post">
  {% csrf_token %}
  {{ form.as_p }}
  <input type="submit" , value="Submit" name="prior_run">
</form>

<form action="" method="post">
  <h2>Variables Table One</h2>

  {% csrf_token %}
  <!-- Table Columns -->
  <table>
    <table class="vartable" style="text-align: center;">
      <colgroup>
        <col class="grey">
      </colgroup>
      <thead>
        <tr>
          <th>Variable 1</th>
          <th>Variable 2</th>
          <th>Variable 3</th>
          <th>Variable 4</th>
          <th>Run Name</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <div>
            <td><input type="text" value="{{inputs_list.0}}" name="var_one"></td>
            <td><input type="text" value="{{inputs_list.1}}" name="var_two"></td>
            <td><input type="text" value="{{inputs_list.2}}" name="var_three"></td>
            <td><input type="text" value="{{inputs_list.3}}" name="var_four"></td>
            <td><input id="run_name" type="text" value="{{inputs_list.5}}" name="run_name"></td>
          </div>
        </tr>
        <tr>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
        </tr>
      </tbody>
    </table>
  </table>

forms.py

class InputsForm(forms.Form):
    past_run_names = [
        (i["run_name"], i["run_name"])
        for i in InputVariables.objects.values("run_name")
    ]
    run_name = forms.ChoiceField(choices=past_run_names)

views.py

class VariableInputView(FormView, View, LoginRequiredMixin):
    template_name = "variable_input.html"
    input_model = InputVariables
    inputs_form_class = InputsForm

    def get(self, request):
       # perform get method

    def post(self, request):
        post_data = request.POST.dict()
            if "prior_run" in request.POST:
                form = self.inputs_form_class(request.POST)
                if form.is_valid():
                   inputs_query = self.input_model.objects.filter(
                       run_name=run_name
                   ).values_list(*first_assumptions_list)
                   inputs_list = list(inputs_query[0])
                   return HttpResponseRedirect(self.request.path_info)
                return render(
                    request,
                    self.template_name,
                    {
                        "inputs_list": inputs_list,
                        "form": form,
                    },
                )
            else:
                 # run the calculation

class BasicTableView(View, LoginRequiredMixin):
    model = None
    template_name = None

    def get(self, request):
        """
        Get the latest entry in the database when the last time the table was
        generated.
        """
        if not request.user.is_authenticated:
            return redirect("/accounts/login")
        else:
                input_var_query = InputVariables.objects.values_list(
                    "var_one",
                    "var_two",
                    "var_three",
                    "var_four",
                    "run_name",
                ).latest("time_stamp")
                input_var_list = list(input_var_query)
                data = (
                    self.model.objects.filter(
                        input_variables__var_one=input_var_list[0],
                        input_variables__var_two=input_var_list[1],
                        input_variables__var_three=input_var_list[2],
                        input_variables__var_four=input_var_list[3],
                        input_variables__run_name=input_var_list[4],
                    )
                    .order_by("time_stamp")
                    .reverse()[: self.end_slice : -1]
                )
                print(data)
            except InputVariables.DoesNotExist:
                ...

            return render(
                request,
                self.template_name,
                {"data": data, "input_var_list": input_var_list},
            )
     

class SummaryOfResultsView(BasicTableView):
    model = SummaryOfResults
    template_name = "summary_of_results.html"

models.py

class SummaryOfResults(models.Model):
    result_one = models.TextField()
    result_two = models.TextField()
    result_three = models.TextField()
    input_variables = models.ForeignKey(InputVariables, on_delete=models.CASCADE)
    time_stamp = models.DateTimeField(auto_now_add=True, blank=True)

class InputVariables(models.Model):
    var_one = models.TextField()
    var_two = models.TextField()
    var_three = models.TextField()
    var_four = models.TextField()
    run_name = models.TextField()
    time_stamp = models.DateTimeField(auto_now=True, null=True)

    def __str__(self):
          # return strings

results.html

{% block content %}
<div class="container">
    <div class="row">

        <!-- Table Columns -->
        <div class="col-md-8 mt-4 left">
            <style>
                .grey {
                    background-color: #dfdede;
                }

                .firsttable th:nth-child(1),
                .firsttable th:nth-child(2),
                .firsttable th:nth-child(3),
                .firsttable tr:last-child td {
                    background-color: #dfdede;
                }
            </style>
            <table class="firsttable" style="text-align: center;" border="1">
                <colgroup>
                    <col class="grey">
                </colgroup>

                <thead>
                    <tr style="font-size: small">
                        <th>Result One</th>
                        <th>Result Two</th>
                        <th>Result Three</th>
                    </tr>
                </thead>

                <tbody>
                    {% for d in data %}
                    <tr style="font-size: small">
                        <td>{{d.result_one}}</td>
                        <td>{{d.result_two}}</td>
                        <td>{{d.result_three}}</td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="5">Currently no Data Available</td>
                    </tr>
                    {% endfor %}
                </tbody>

            </table>

        </div>
        {% block sidebar %} {% include 'sidebar.html' %} {% endblock sidebar %}
        <div class="col-md-8 mt-3 left">
            <style>
                .grey {
                    background-color: #dfdede;
                }

                .CurrentVariableTable th:nth-child(1),
                .CurrentVariableTable th:nth-child(2),
                .CurrentVariableTable th:nth-child(3),
                .CurrentVariableTable th:nth-child(4),
                .CurrentVariableTable th:nth-child(5) {
                    background-color: #dfdede;
                }
            </style>
            <table class="CurrentVariableTable" style="text-align: center;" border="1">
                <thead>
                    <tr style="font-size: small">
                        <th>Variable One</th>
                        <th>Variable Two</th>
                        <th>Variable Three</th>
                        <th>Variable Four</th>
                    </tr>
                </thead>
                <tbody>
                    <tr style="font-size: small">
                        <td>{{input_var_list.0}}%</td>
                        <td>{{input_var_list.1}}%</td>
                        <td>{{input_var_list.2}}%</td>
                        <td>{{input_var_list.3}}%</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %}

Have you tried template tags?

you write it once, but you could use it everywhere in templates.

I will give you an example:

register = template.Library()


@register.simple_tag(name='get_live_jobs')
def get_live_jobs():
    live_jobs = Job.objects.all().exclude(
        Q(is_deleted=True) | Q(last_date__lt=datetime.date.today()) | Q(is_archived=True)).count()
    return live_jobs

then in your template, you will do:

{% load get_general_counters %}
{% get_live_jobs %}
Back to Top