Home > other >  Django missing data from database
Django missing data from database

Time:03-10

Having a problem that shouldn't appear. Created a django project. all the steps on windows OS. Now when went to do everything the same on Ubuntu 20.04 then my index.html is not displaying any data from database even if there is a data ( i checked )

Html::

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Django CRUD Operations</title>
  <meta charset="utf-8">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> 
</head>
<body>
<div >
<table >
    <thead>
      <tr>
        <th>Flight ID</th>
        <th>Aircraft</th>
        <th>Registration</th>
        <th>Latitude</th>
        <th>Longtitude</th>
      </tr>
    </thead>
    <tbody>
    {% for c in flight %}  
      <tr>
        <td>{{c.id}}</td>
        <td>{{c.aircraft}}</td>
        <td>{{c.registration}}</td>
        <td>{{c.latitude}}</td>
        <td>{{c.longtitude}}</td>
      </tr>
      {% endfor %} 
    </tbody>
</table>    
</div>
</body>
</html>

Views.py::

from .models import Flights
from django.shortcuts import render

def show(request):
    flights = Flights.objects.all()
    return render(request,"show.html",{'flight':flights})

Database is connected. Any ideas? Maybe Ubuntu 20.04 has something that have to do differently like windows os?

enter image description here

UPDATE:

Here is my settings.py :

"""
Django settings for djangoproject project.

Generated by 'django-admin startproject' using Django 3.2.4.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '******'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'fly',
    'django',
]

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 = 'djangoproject.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 = 'djangoproject.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'flight_radar_db',
        'USER': 'postgres',
        'PASSWORD': '*******',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators

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',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/

STATIC_URL = '/static/'

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

Models.py::

from django.db import models

# Create your models here.
class Flights(models.Model):
    aircraft = models.CharField(max_length=60)
    registration = models.CharField(max_length=60)
    altitude = models.CharField(max_length=60)
    ground_speed = models.CharField(max_length=60)
    heading = models.CharField(max_length=60)
    latitude = models.CharField(max_length=60)
    longtitude = models.CharField(max_length=60)
    flying_from = models.CharField(max_length=60)
    flying_to = models.CharField(max_length=60)
    distance = models.CharField(max_length=60)

    def __str__(self):
        return self.aircraft

CodePudding user response:

I think your access to flights object is wrong

as said Here you must access it like this

  {% for c in flights['flight'] %}  
  <tr>
    <td>{{c.id}}</td>
    <td>{{c.aircraft}}</td>
    <td>{{c.registration}}</td>
    <td>{{c.latitude}}</td>
    <td>{{c.longtitude}}</td>
  </tr>
  {% endfor %} 
   

or you look it up like this

{% for key, value in flights %}
    <p> Value {{ value }}</p>
{% endfor %}

CodePudding user response:

Query the database using Django-shell, and test the data. Use python3 manage.py shell. In the shell:

from fly.models import Flights

flight_qs = Flights.objects.all()

for c in flight_qs:
    print(c.id)

If you see the results, there is no database issue then.

  • Related