简体   繁体   English

Django 在模板中打印已在 python 中传递给它的变量

[英]Django printing variable in template that has been passed to it in python

I am trying to get 'date' which can be eg 2023/01/29 to print in my template file.我正在尝试获取“日期”,例如 2023/01/29 以在我的模板文件中打印。

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.该代码当前检查变量“date”是否存在并且不是 -1,但我然后 go 打印它并且内容没有出来。

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.我尝试使用一组 {} 以及使用 print(content[date]),但变量再次不是 output。我试图将其设置为 output 之前输入的日期。

Any help would be greatly apricated.任何帮助都将得到极大的帮助。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM