Answer the question
In order to leave comments, you need to log in
How to display data from db to django html?
After adding an entry to the database (through the db browser, admin panel, or ORM - it doesn’t matter), Django on the html page does not update the data even when the page is reloaded, although everything is added to the database. The entry added to the database earlier will appear on html only if you restart the entire project via manage.py runserver
views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic import ListView, DetailView
from itertools import chain
from viewNV08C.models import GNSSData
from viewNV08C.models import DriverStatus
object_list= list(chain(GNSSData.objects.all().order_by("-date").reverse()[:1], DriverStatus.objects.all().order_by("id").reverse()[:1]))
def driverDataListView(request):
return render(request, 'viewNV08C/driver.html', {'object_list':object_list})
def dbDataListView(request):
return render(request, 'viewNV08C/db.html', {'object_list':object_list})
def homeDataListView(request):
return render(request, 'viewNV08C/homePage.html', {'object_list':object_list})
from django.conf.urls import url
from . import views
from itertools import chain
from django.views.generic import ListView, DetailView
from viewNV08C.models import GNSSData
from viewNV08C.models import DriverStatus
urlpatterns = [
url(r'^$', views.homeDataListView, name = 'home'),
url(r'^db/', views.dbDataListView, name = 'db'),
url(r'^driver/', views.driverDataListView, name = 'driver')
]
{% extends "viewNV08C/wrapper.html" %}
{% block content %}
<p2><b>Driver Data:</b></p2>
<p>Status: {% for val in object_list %}{{val.status}}{% endfor %}</p>
{% endblock %}
from django.db import models
class GNSSData(models.Model):
time = models.CharField(max_length=8)
coordinate = models.CharField(max_length=50)
date = models.CharField(max_length=15)
satellitesCount = models.CharField(max_length=2)
dataFlag = models.CharField(max_length=15)
class DriverStatus(models.Model):
status = models.CharField(max_length=100)
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = True
ALLOWED_HOSTS = ['10.11.5.108', '127.0.0.1']
INSTALLED_APPS = [
'viewNV08C',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'view.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'view.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question