Hi Django Community,
I hope you’re all doing well. I’m currently working on a Django project and have encountered an issue that’s been puzzling me for a while now. I’m reaching out to seek your expertise and assistance in resolving it.
The Problem: Despite everything seeming to work fine, the content from my database is not displaying on the webpages when I run the development server. Initially, when I create the website, all content loads perfectly. However, upon returning the next day, none of the database content appears on the pages. This issue persists both in my local development environment and when using Docker.
What I’ve Tried:
- I’ve thoroughly checked my code to ensure there are no obvious errors.
- I’ve restarted the server multiple times, but the issue persists.
- I’ve tested both locally and with Docker, experiencing the same problem in both environments.
- I’ve verified that the database connection is established correctly.
What I Need Help With: I’m at a loss as to what might be causing this issue. I’m seeking advice on where to look next and how to troubleshoot effectively.
“”"
Django settings for dj project.
Generated by ‘django-admin startproject’ using Django 5.0.2.
For more information on this file, see
For the full list of settings and their values, see
“”"
import os
from pathlib import Path
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 Deployment checklist | Django documentation | Django
SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = “django-insecure-bmh)9+a7$%k)c7g-y!ns_%fby053fs_y2(!+u5a1*–_n@5bs2”
SECURITY WARNING: don’t run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = [“localhost”, “0.0.0.0”]
Application definition
INSTALLED_APPS = [
“django.contrib.admin”,
“django.contrib.auth”,
“django.contrib.contenttypes”,
“django.contrib.sessions”,
“django.contrib.messages”,
“django.contrib.staticfiles”,
“services.apps.ServicesConfig”,
“location.apps.LocationConfig”,
“expense.apps.ExpenseConfig”,
“about.apps.AboutConfig”,
“pages.apps.PagesConfig”,
]
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 = “dj.urls”
TEMPLATES = [
{
“BACKEND”: “django.template.backends.django.DjangoTemplates”,
“DIRS”: [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 = “dj.wsgi.application”
Database
Settings | Django documentation | Django
DATABASES = {
“default”: {
“ENGINE”: “django.db.backends.sqlite3”,
“NAME”: BASE_DIR / “db.sqlite3”,
}
}
DATABASES = {
“default”: {
“ENGINE”: “django.db.backends.postgresql”,
“NAME”: “postgres”,
“USER”: “postgres”,
“PASSWORD”: “postgres”,
“HOST”: “db”,
“PORT”: 5432,
}
}
Password validation
Settings | Django documentation | Django
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
Internationalization and localization | Django documentation | Django
LANGUAGE_CODE = “en-us”
TIME_ZONE = “UTC”
USE_I18N = True
USE_TZ = True
Static files (CSS, JavaScript, Images)
How to manage static files (e.g. images, JavaScript, CSS) | Django documentation | Django
STATIC_URL = “/static/”
STATICFILES_DIRS = [os.path.join(BASE_DIR, “static”)]
STATIC_ROOT = os.path.join(BASE_DIR, “staticfiles”)
STATICFILES_STORAGE = “whitenoise.storage.CompressedManifestStaticFilesStorage”
MEDIA_URL = “/media/”
MEDIA_ROOT = os.path.join(BASE_DIR, “media”)
Default primary key field type
Settings | Django documentation | Django
DEFAULT_AUTO_FIELD = “django.db.models.BigAutoField”
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path(“admin/”, admin.site.urls),
path(“”, include(“pages.urls”)),
path(“about/”, include(“about.urls”)),
path(“location/”, include(“location.urls”)),
path(“service/”, include(“services.urls”)),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
{% extends “_base.html” %} {% block content %}
Rachel's Nails & Make Up Art!
{{ post.name }}
{{ post.about }}
{% endblock content %}
from django.db import models
class Post(models.Model):
name = models.CharField(max_length=100, blank=True)
about = models.TextField()
image = models.ImageField(upload_to=“uploads”)
def __str__(self):
return self.name[:50]
from django.views.generic import ListView
from .models import Post
class HomePostView(ListView):
model = Post
template_name = “home.html”
context_object_name = “post_list”
Pull base image
FROM python:3.11-slim
Set environment variables
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
Set work directory
WORKDIR /code
Install dependencies
COPY ./requirements.txt .
RUN pip install -r requirements.txt
Copy project
COPY . .
version: “3.9”
services:
web:
build: .
command: python /code/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- 8000:8000
depends_on:
- db
db:
image: postgres:13
volumes:
- postgres_data:/var/lib/docker/volumes/rounds_postgres_data/_data
# - postgres_data:/var/lib/postgresql/data/
environment:
- “POSTGRES_HOST_AUTH_METHOD=trust”
volumes:
postgres_data:
2024-02-25 19:20:00 web-1 | Django version 5.0.2, using settings ‘dj.settings’
2024-02-25 19:20:00 web-1 | Starting development server at http://0.0.0.0:8000/
2024-02-25 19:20:00 web-1 | Quit the server with CONTROL-C.
2024-02-25 19:20:00 web-1 |
2024-02-25 19:20:02 web-1 | [25/Feb/2024 16:20:02] “GET / HTTP/1.1” 200 1196
2024-02-25 19:20:05 web-1 | [25/Feb/2024 16:20:05] “GET /service/service/ HTTP/1.1” 200 1282
2024-02-25 19:35:58 web-1 | [25/Feb/2024 16:35:58] “GET / HTTP/1.1” 200 1196
2024-02-25 19:36:03 web-1 | [25/Feb/2024 16:36:03] “GET /location/loc/ HTTP/1.1” 200 1201
2024-02-25 19:36:05 web-1 | [25/Feb/2024 16:36:05] “GET /about/about/ HTTP/1.1” 200 1222
2024-02-25 19:36:08 web-1 | [25/Feb/2024 16:36:08] “GET /about/about/ HTTP/1.1” 200 1222
Errors I’m Encountering:
Unfortunately, I haven’t received any specific error messages. The issue seems to be more of a content not displaying problem rather than a crash or error. However, I’m open to suggestions on how to capture and share any potential error messages or logs that might be helpful in diagnosing the issue.
Any guidance or insights you can offer would be greatly appreciated. Thank you in advance for your help!
Best regards,
Mom’s Basement!