Home > Net >  Django printing variable in template that has been passed to it in python
Django printing variable in template that has been passed to it in python

Time:01-31

I am trying to get 'date' which can be eg 2023/01/29 to print in my template file.

def time_booking(request):
    if 'date' in request.COOKIES:
        context = {
            'date':request.COOKIES['date'],
        }
        print("Run Run")
        return render(request, "path_to_file.html", context)
        <h1>Date will be here here</h1>
        {% if date != -1 or null %}
        <h1> Date is real {{date}}</h1>
        {% else %}
        <h1> Date is not real{{date}} </h1>
        {% endif %}
        <h1> complete run </h1>

The code currently check if the variable 'date' exists and isn't -1 but I then go to print it and the content doesn't come out.

I tried using a single set of {} as well as using print(content[date]), but again the variable was not output. I am trying to get it to output the date which has been entered before hand.

Any help would be greatly apricated.

CodePudding user response:

Try updating your code as follows:

def time_booking(request):
    if 'date' in request.COOKIES:
    context = {
    'date':request.COOKIES['date'],
    }
    print("Run Run")
    else:
    context = {
    'date':None
    }
    return render(request, "path_to_file.html", context)
    

And in your template:

    <h1>Date will be here here</h1>
    {% if date %}
    <h1> Date is real {{date}}</h1>
    {% else %}
    <h1> Date is not real{{date}} </h1>
    {% endif %}
    <h1> complete run </h1>
  • Related