How to always display model data in Django template after reloading a page?

guten_pro

Scenario:

Currently, my Template has a text which says "Last seen" and a timer that mentions the time generated after clicking a button. If a new user visits the site, there will be no time displayed. If a user clicks on the button, the time will be generated and created in the model. And every time a user refreshes the page, the old/current time should be displayed near the text. Right now, every time I reload the page, there is no time displayed despite being saved in my model. How can I show the current time in my Template after reloading each time?

This is what I have tried out so far:

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 %}

As stated, after each page reload, the time is displayed as 'None' and I have to keep clicking the button for the time to show up. Is there a way to view the time (if existing) after each reload?

Rupin

Welcome to StackOverflow.

You need to send a context back to the template

Here is how your view should change

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)) 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to display Model data in a Django Template

How to update django template variable without reloading page (AJAX)?

How can i display alert after sending form and reloading page?

How can I make a page that will always display the latest post from my Django Model

How to display model fuction in Django template

Django - How to display parent model property in template

How to display in template data from model?

Django template won't display model object attributes on page

Display Data on Dialogbox without reloading page

iOS: Slide to display data after reloading UITableView

How to display data from model to razor page

how to display model data to html page

Ajax page is reloading after storing data

Django: How to render model data into HTML template?

Django// How to display complex depth data in template

Django: How display effectively data in my template?

How to call useEffect after page reload ? After reloading my state (data) is show an empty array

How to save the value of the button after reloading the page?

How to remember the value of a variable after reloading the page?

How to keep value displayed after reloading of the page?

How to retain the pagination number, after reloading the page

How not to reset var after a page reloading

How to display Foreignkey Data in Django html page?

How to insert data to a template after render ? (django)

how to filter data on button click and display on the same tab without reloading the page

How to display the information contained in a related model's field in Django template

how to paginate data without reloading page?

How to display several matplotlib plots in one django template page

Reloading the page after redirect