From c80573e56757c7cbd4a5a2850400ada7ad952f51 Mon Sep 17 00:00:00 2001 From: Jonathan Rampersad Date: Thu, 29 May 2025 15:43:13 -0400 Subject: [PATCH] Added template and view that uses static assets --- README.md | 2 +- .../templates/static_app/index.html | 19 +++++++++++++++++++ static_project/static_app/urls.py | 6 ++++++ static_project/static_app/views.py | 2 ++ static_project/static_project/settings.py | 8 ++++++++ static_project/static_project/urls.py | 3 ++- 6 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 static_project/static_app/templates/static_app/index.html create mode 100644 static_project/static_app/urls.py diff --git a/README.md b/README.md index d4f179d..20c92bb 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ # Django-Inventory-App -This repo is document my journey of learning Django by building an Inventory App \ No newline at end of file +This repo is document my journey of learning how Django deals with static assets \ No newline at end of file diff --git a/static_project/static_app/templates/static_app/index.html b/static_project/static_app/templates/static_app/index.html new file mode 100644 index 0000000..6116e04 --- /dev/null +++ b/static_project/static_app/templates/static_app/index.html @@ -0,0 +1,19 @@ +{% load static %} + + + + + + Django is Awesome + + + + Django Logo + +

Welcome to Django!

+

Django makes it easier to build better web apps faster and with less code.

+ + + + + diff --git a/static_project/static_app/urls.py b/static_project/static_app/urls.py new file mode 100644 index 0000000..55777f9 --- /dev/null +++ b/static_project/static_app/urls.py @@ -0,0 +1,6 @@ +from django.urls import path +from .views import index + +urlpatterns = [ + path('', index, name='index'), +] \ No newline at end of file diff --git a/static_project/static_app/views.py b/static_project/static_app/views.py index 91ea44a..2701c12 100644 --- a/static_project/static_app/views.py +++ b/static_project/static_app/views.py @@ -1,3 +1,5 @@ from django.shortcuts import render # Create your views here. +def index(request): + return render(request, 'static_app/index.html') diff --git a/static_project/static_project/settings.py b/static_project/static_project/settings.py index 6886079..6418569 100644 --- a/static_project/static_project/settings.py +++ b/static_project/static_project/settings.py @@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/5.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 @@ -37,6 +38,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'static_app.apps.StaticAppConfig', ] MIDDLEWARE = [ @@ -116,6 +118,12 @@ USE_TZ = True STATIC_URL = 'static/' +# MEDIA_URL = 'images/' + +STATICFILES_DIRS = [ + os.path.join(BASE_DIR, "static"), +] + # Default primary key field type # https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field diff --git a/static_project/static_project/urls.py b/static_project/static_project/urls.py index 0d5682f..41aa4b3 100644 --- a/static_project/static_project/urls.py +++ b/static_project/static_project/urls.py @@ -15,8 +15,9 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), + path('', include('static_app.urls')) ]