Home > Blockchain >  Django manipulate / modify date in template
Django manipulate / modify date in template

Time:09-03

Is it possible to make an input that changes the date.today for the whole template ?

my template

{% for Ansicht in Ansicht.lehrertabelle_set.all  %} 
  <tbody>
    <tr>
    <th scope="row"></th>
    <td>{{Ansicht.Pflichtstunden_normal}}</td>
    <td>{{Ansicht.Status_normal}}</td>
    {% if Ansicht.Prognose_FK %} 
    <td>{{Ansicht.Prognose_FK.Status}}</td>
    <td>{{Ansicht.Prognose_FK.Stunden}}</td>
    {% else %}
    <td>{{Ansicht.Prognose_FK.Status_2}}</td>
    <td>{{Ansicht.Prognose_FK.Stunden_2}}</td>
    {% endif %}
{% endfor %}

the filter then would show those

    <td>{{Ansicht.Prognose_FK.Status_2}}</td>
    <td>{{Ansicht.Prognose_FK.Stunden_2}}</td>

instead of the first ones when the date is modified, I tried to use javascript but I guess it dont work bc of python objects

my model is

class PrognoseTabelle(models.Model):
    Benutzer = models.ForeignKey(User, on_delete=models.SET_NULL, null=True) 
    Lehrer_FK = models.ForeignKey('LehrerTabelle', on_delete=models.SET_NULL, related_name='LehrerNName', null=True, blank=True)
    #######Status 1######
    von_Datum = models.DateField(null=True, blank=True)
    Status = models.CharField(max_length=20, null=True, blank=True)
    Stunden = models.CharField(max_length=20, null=True, blank=True)
    bis_Datum = models.DateField(null=True, blank=True)
    #######Status 2######
    von_Datum_2 = models.DateField(null=True, blank=True)
    Status_2 = models.CharField(max_length=20, null=True, blank=True)
    Stunden_2 = models.CharField(max_length=20, null=True, blank=True)
    bis_Datum_2 = models.DateField(null=True, blank=True)
    #######Status 3######

CodePudding user response:

It is impossible to influence the rendered HTML from only server-side code, after it has been rendered and displayed by the user. If I understand you correctly, your intended user interaction with this view/menu looks kind of like this:

  1. User action: The user opens the menu.
  2. Program action: Among other things, a date-input is displayed.
  3. User action: The user inputs a date using the date-input.
  4. Program action: The displayed content changes.

Given, that you cannot influence the rendered HTML with only server-side code in step 4., you have two possibilities to achieve this:

  1. display new rendered HTML
  2. use client-side code (for example JavaScript)

I'm going to explain the first possibility here:

It's probably the easiest to use a django form to render the input. forms.py:

from django import forms
from django.utils.timezone import localdate

class PrognoseFilterForm(forms.Form):
    """Form for filtering the Prognose table."""
    datum = forms.DateField()

somewhere in views.py:

def prognose_tabelle_view(request, *args, **kwargs):
    form = PrognoseFilterForm(request.GET, initial={'date': timezone.localdate()})
    datum = timezone.localdate()
    if form.is_bound() and form.is_valid():
        datum = form.cleaned_data['datum']        

    prognose_query = Ansicht.lehrertabelle_set.all()
    prognoseansichten_bis_einschliesslich_datum = prognose_query.filter(
        Prognose_FK__von_Datum__lte=datum
    )
    prognoseansichten_ab_datum = prognose_query.filter(
        Prognose_FK__von_Datum__gt=datum
    )
    
    return render(
        request,
        'your_app/prognose_tabelle.html',
        context={
            'filter_form': form,
            'prognosen_bis_datum': prognoseansichten_bis_einschliesslich_datum,
            'prognosen_ab_datum': prognoseansichten_ab_datum,
        }
    )

prognose_tabelle.html:

<form action="{% url '<name of the view according to urls.py>' %}" method="get">
  {{ filter_form.as_p }}
  <input type="submit">Filtern</input>
</form>
<table>
  <tbody>
    {% for Ansicht in prognosen_bis_datum %}
      <tr>
        <th scope="row"></th>
        <td>{{Ansicht.Pflichtstunden_normal}}</td>
        <td>{{Ansicht.Status_normal}}</td>
        <td>{{Ansicht.Prognose_FK.Status}}</td>
        <td>{{Ansicht.Prognose_FK.Stunden}}</td>
      </tr>
    {% endfor %}
    {% for Ansicht in prognosen_ab_datum %}
      <tr>
        <th scope="row"></th>
        <td>{{Ansicht.Pflichtstunden_normal}}</td>
        <td>{{Ansicht.Status_normal}}</td>
        <td>{{Ansicht.Prognose_FK.Status_2}}</td>
        <td>{{Ansicht.Prognose_FK.Stunden_2}}</td>
      </tr>
    {% endfor %}
  </tbody>
</table>

When a user clicks the button labelled with "Filtern", the form data is sent to the view via an HTTP-Get-Request in its GET-Parameters. The server then responds with a new HTML page, which the browser then displays to the user.

  • Related