Django模板重新渲染

时间:2019-09-16 08:46:22

标签: python django django-forms django-templates

因此,我有一个django DetailView,可以通过在基于类的视图中从get_context_data获取所需的所有数据来轻松呈现。但是问题在于,每次渲染此模板时,我都需要在该模板中选择一个日期,以某种方式获取日期,然后从选择的日期重新渲染该模板。

select option

我一直在研究如何实现这一目标,我可以使用AJAX进行某些操作,但也许可以尝试一些更简单的方法,因为我对AJAX / JS的使用经验很少甚至没有经验

class PlaceDetailView(LoginRequiredMixin, PermissionCheckMixin, PlaceViewMixin, DetailView):
    template_name = "places/place_detail.html"

    def get_context_data(self, **kwargs):
        context = super(PlaceDetailView, self).get_context_data(**kwargs)
        place = context.get("object")

        contract = Contract.objects.filter(pk=place.id).first()
        context["renter"] = User.objects.filter(pk=contract.renter_id).first()

        now = timezone.now()

        meters = MeterInstallation.objects.filter(places=place.id)
        context["active_meters"] = meters.filter(active_until__gte=now)
        context["old_meters"] = meters.filter(active_until__lt=now)

        context["services"] = ServiceTask.objects.filter(place=place)

        # Need to add more context values from more models, but the date has to be given, how?

        return context


place_detail_view = PlaceDetailView.as_view()
<div class="tab-pane fade show active" id="values" role="tabpanel" aria-labelledby="values-tab">
        {% if values %}
          <div class="table-responsive-sm">
            <table class="table small">
              <thead>
              <tr>
                <th scope="col">#</th>
                <th scope="col">Values</th>
              </tr>
              </thead>
              <tbody>

              {% for value in values %}
                <tr>
                  <th scope="row">{{ forloop.counter }}</th>
                  <!-- todo add needed values (type, values from/to, diff, tariff, total_price) -->
                </tr>
              {% endfor %}

              </tbody>
            </table>
          </div>
        {% else %}
          No values.
        {% endif %}
    </div>

我想知道在选择日期时是否可以重新渲染此模板?或者也许有更好的选择可以实现这一目标?:)

0 个答案:

没有答案