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?
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.
Comments