Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在重新加载页面后始终在Django模板中显示模型数据?_Python_Django - Fatal编程技术网

Python 如何在重新加载页面后始终在Django模板中显示模型数据?

Python 如何在重新加载页面后始终在Django模板中显示模型数据?,python,django,Python,Django,情景: 目前,我的模板有一个文本,上面写着“最后一次看到”,还有一个计时器,上面写着单击按钮后生成的时间。如果新用户访问该站点,则不会显示时间。如果用户单击按钮,将在模型中生成和创建时间。每次用户刷新页面时,旧/当前时间应显示在文本附近。现在,每次我重新加载页面时,都没有显示时间,尽管它保存在我的模型中。每次重新加载后,如何在模板中显示当前时间 这是我迄今为止尝试过的: models.py: from django.contrib.auth.models import User class P

情景:

目前,我的模板有一个文本,上面写着“最后一次看到”,还有一个计时器,上面写着单击按钮后生成的时间。如果新用户访问该站点,则不会显示时间。如果用户单击按钮,将在模型中生成和创建时间。每次用户刷新页面时,旧/当前时间应显示在文本附近。现在,每次我重新加载页面时,都没有显示时间,尽管它保存在我的模型中。每次重新加载后,如何在模板中显示当前时间

这是我迄今为止尝试过的:

models.py:

from django.contrib.auth.models import User

class Path(models.Model):
    user = models.OneToOneField(User, on_delete = models.CASCADE)
    current_time = models.DateTimeField(blank=True, null=True)  #can be Null or blank if users enters the site for the first time.
views.py:

from django.utils import timezone 
import pytz
from django.http import JsonResponse, HttpResponse

def Func():
    '''
    If the current_time does not exist, then we create it in the Path model.
    #If time already exists, then we grab the id and replace current time with a new one using the update() method. 
    '''
    time = timezone.now()  #Format: 2019-11-16 14:53:35.774824+00:00
    user_id = request.user.id      #User id = 1
    if (Path.objects.filter(current_time = time) in (None, '')): 
        time = Path.objects.create(current_time = time)
    else:
        time = Path.objects.filter(user_id=user_id).update(current_time = time) 
    obj = Path.objects.values_list('current_time', flat=True)
    return HttpResponse(obj)  
home.html:

... HTML stuff here
#If time exists in model, then display it in Template (even after refreshing/reloading)
{% if obj.current_time %}
    <div class="placeRight">
        <p> Last indexed:
            <b> {{obj.current_time}}</b> 
        </p> 
    </div>
#If time doesn't exist, then display None (this will only occur if a user enters the site for the first time). 
{% else %}
    <div class="placeRight">
        <p> Last indexed:
            <b> None </b> 
        </p> 
    </div> -->
{% endif %}
。。。这里的HTML内容
#如果模型中存在时间,则在模板中显示它(即使在刷新/重新加载之后)
{%if obj.current_time%}
上次索引:
{{obj.current_time}

#如果时间不存在,则显示无(仅当用户第一次进入站点时才会出现)。 {%else%} 上次索引: 没有一个

--> {%endif%}

如上所述,每次重新加载页面后,时间显示为“无”,我必须不断单击按钮以显示时间。是否有办法查看每次重新加载后的时间(如果存在)?

欢迎使用StackOverflow

您需要将上下文发送回模板

以下是你的观点应该如何改变

from django.template import loader # I added this line

    def Func():
    '''
    If the current_time does not exist, then we create it in the Path model.
    #If time already exists, then we grab the id and replace current time with a new one using the update() method. 
    '''
    time = timezone.now()  #Format: 2019-11-16 14:53:35.774824+00:00
    template = loader.get_template('home.html')   # I added this line
    user_id = request.user.id      #User id = 1
    if (Path.objects.filter(current_time = time) in (None, '')): 
        time = Path.objects.create(current_time = time)
    else:
        time = Path.objects.filter(user_id=user_id).update(current_time = time) 
    obj = Path.objects.values_list('current_time', flat=True)

    context = {

            'obj':obj
    }

    return HttpResponse(template.render(context, request)) 

谢谢你的回复。你能解释一下你做了什么吗?我在你的原始代码中添加了几行代码。导入加载程序,加载模板,创建上下文字典,将上下文应用于模板,并将其发送到浏览器。