diff --git a/Atlas/Engineering/Programming/Python/Django/Manage commands.md b/Atlas/Engineering/Programming/Python/Django/Manage commands.md new file mode 100644 index 0000000..73cbee9 --- /dev/null +++ b/Atlas/Engineering/Programming/Python/Django/Manage commands.md @@ -0,0 +1,10 @@ +`startap myapp` - create application named `myapp` +`shell` - open Python REPL in the project context +`createsuperuser` create an admin super user +`runserver` - run dev server + +### Migrations + +`makemigrations name` - create a new SQL migration based on model file +`sqlmigrate blog 001` - open a SQL migration blog number 001 +`migrate` - apply migrations diff --git a/Projects/Testtrack/M1 - Django Learning/README.md b/Projects/Testtrack/M1 - Django Learning/README.md new file mode 100644 index 0000000..30a9833 --- /dev/null +++ b/Projects/Testtrack/M1 - Django Learning/README.md @@ -0,0 +1,10 @@ +# M1 - Django Learning + +Sandbox project for learning Django framework.primarly based on the [Django 5 by example book](https://djangobyexample.com/). + +## Sources + +- [Django 5 by example book](https://djangobyexample.com/) +- [Examples source code](https://github.com/PacktPublishing/Django-5-By-Example) +- [Django documentation](https://docs.djangoproject.com/en/5.2//) +- [Django Design philosophy](https://docs.djangoproject.com/en/5.2/misc/design-philosophies/) diff --git a/Projects/Testtrack/M1 - Django Learning/mysite/blog/__init__.py b/Projects/Testtrack/M1 - Django Learning/mysite/blog/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Projects/Testtrack/M1 - Django Learning/mysite/blog/admin.py b/Projects/Testtrack/M1 - Django Learning/mysite/blog/admin.py new file mode 100644 index 0000000..a6157cd --- /dev/null +++ b/Projects/Testtrack/M1 - Django Learning/mysite/blog/admin.py @@ -0,0 +1,14 @@ +from django.contrib import admin +from .models import Post + + +@admin.register(Post) +class PostAdmin(admin.ModelAdmin): + list_display = ('title', 'slug', 'author', 'published_at', 'status') + list_filter = ('status', 'created_at', 'published_at', 'author') + search_fields = ('title', 'body') + prepopulated_fields = {'slug': ('title',)} + raw_id_fields = ('author',) + date_hierarchy = 'published_at' + ordering = ('status', 'published_at') + show_facets = admin.ShowFacets.ALWAYS diff --git a/Projects/Testtrack/M1 - Django Learning/mysite/blog/apps.py b/Projects/Testtrack/M1 - Django Learning/mysite/blog/apps.py new file mode 100644 index 0000000..94788a5 --- /dev/null +++ b/Projects/Testtrack/M1 - Django Learning/mysite/blog/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class BlogConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'blog' diff --git a/Projects/Testtrack/M1 - Django Learning/mysite/blog/migrations/0001_initial.py b/Projects/Testtrack/M1 - Django Learning/mysite/blog/migrations/0001_initial.py new file mode 100644 index 0000000..643dddf --- /dev/null +++ b/Projects/Testtrack/M1 - Django Learning/mysite/blog/migrations/0001_initial.py @@ -0,0 +1,40 @@ +# Generated by Django 5.2.7 on 2025-10-05 10:34 + +import django.db.models.deletion +import django.utils.timezone +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Post', + fields=[ + ('id', models.BigAutoField(auto_created=True, + primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=250)), + ('slug', models.SlugField(max_length=250)), + ('body', models.TextField()), + ('published_at', models.DateTimeField( + default=django.utils.timezone.now)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('updated_at', models.DateTimeField(auto_now=True)), + ('status', models.CharField(choices=[ + ('DF', 'Draft'), ('PB', 'Published')], default='DF', max_length=2)), + ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, + related_name='blog_posts', to=settings.AUTH_USER_MODEL)), + ], + options={ + 'ordering': ('-published_at',), + 'indexes': [models.Index(fields=['-published_at'], name='blog_post_publish_2c9212_idx')], + }, + ), + ] diff --git a/Projects/Testtrack/M1 - Django Learning/mysite/blog/migrations/__init__.py b/Projects/Testtrack/M1 - Django Learning/mysite/blog/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Projects/Testtrack/M1 - Django Learning/mysite/blog/models.py b/Projects/Testtrack/M1 - Django Learning/mysite/blog/models.py new file mode 100644 index 0000000..9d8e590 --- /dev/null +++ b/Projects/Testtrack/M1 - Django Learning/mysite/blog/models.py @@ -0,0 +1,41 @@ +from django.db import models +from django.utils import timezone +from django.conf import settings + + +class PublishedManager(models.Manager): + def get_queryset(self): + return super().get_queryset().filter(status=Post.Status.PUBLISHED) + + +class Post(models.Model): + + class Status(models.TextChoices): + DRAFT = 'DF', 'Draft' + PUBLISHED = 'PB', 'Published' + + title = models.CharField(max_length=250) + slug = models.SlugField(max_length=250) + author = models.ForeignKey( + settings.AUTH_USER_MODEL, + on_delete=models.CASCADE, + related_name='blog_posts' + ) + body = models.TextField() + published_at = models.DateTimeField(default=timezone.now) + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) + status = models.CharField( + max_length=2, choices=Status.choices, default=Status.DRAFT) + + objects = models.Manager() + published = PublishedManager() + + class Meta: + ordering = ('-published_at',) + indexes = [ + models.Index(fields=['-published_at']), + ] + + def __str__(self): + return self.title diff --git a/Projects/Testtrack/M1 - Django Learning/mysite/blog/static/css/styles.css b/Projects/Testtrack/M1 - Django Learning/mysite/blog/static/css/styles.css new file mode 100644 index 0000000..a0a3a89 --- /dev/null +++ b/Projects/Testtrack/M1 - Django Learning/mysite/blog/static/css/styles.css @@ -0,0 +1,97 @@ +body { + margin:0; + padding:0; + font-family:helvetica, sans-serif; +} + +a { + color:#00abff; + text-decoration:none; +} + +h1 { + font-weight:normal; + border-bottom:1px solid #bbb; + padding:0 0 10px 0; +} + +h2 { + font-weight:normal; + margin:30px 0 0; +} + +#content { + float:left; + width:60%; + padding:0 0 0 30px; +} + +#sidebar { + float:right; + width:30%; + padding:10px; + background:#efefef; + height:100%; +} + +p.date { + color:#ccc; + font-family: georgia, serif; + font-size: 12px; + font-style: italic; +} + +.left { + float:left; + margin-right:10px; +} + +/* pagination */ +.pagination { + margin:40px 0; + font-weight:bold; +} + +/* forms */ +label { + float:left; + clear:both; + color:#333; + margin-bottom:4px; +} +input, textarea { + clear:both; + float:left; + margin:0 0 10px; + background:#ededed; + border:0; + padding:6px 10px; + font-size:12px; +} +input[type=submit] { + font-weight:bold; + background:#00abff; + color:#fff; + padding:10px 20px; + font-size:14px; + text-transform:uppercase; +} +.errorlist { + color:#cc0033; + float:left; + clear:both; + padding-left:10px; +} + +/* comments */ +.comment { + padding:10px; +} +.comment:nth-child(even) { + background:#efefef; +} +.comment .info { + font-weight:bold; + font-size:12px; + color:#666; +} diff --git a/Projects/Testtrack/M1 - Django Learning/mysite/blog/templates/blog/base.html b/Projects/Testtrack/M1 - Django Learning/mysite/blog/templates/blog/base.html new file mode 100644 index 0000000..2e4bba1 --- /dev/null +++ b/Projects/Testtrack/M1 - Django Learning/mysite/blog/templates/blog/base.html @@ -0,0 +1,19 @@ +{% load static %} + + + + + + {% block title %}{% endblock %} + + + +
+ {% block content %}{% endblock %} +
+ + + diff --git a/Projects/Testtrack/M1 - Django Learning/mysite/blog/templates/blog/post/detail.html b/Projects/Testtrack/M1 - Django Learning/mysite/blog/templates/blog/post/detail.html new file mode 100644 index 0000000..73f1030 --- /dev/null +++ b/Projects/Testtrack/M1 - Django Learning/mysite/blog/templates/blog/post/detail.html @@ -0,0 +1,11 @@ +{% extends "blog/base.html" %} + +{% block title %}{{ post.title }}{% endblock %} + +{% block content %} +

{{ post.title }}

+

+ Published on {{ post.published_at }} by {{post.author}} +

+ {{ post.body|linebreaks }}

+{% endblock %} diff --git a/Projects/Testtrack/M1 - Django Learning/mysite/blog/templates/blog/post/list.html b/Projects/Testtrack/M1 - Django Learning/mysite/blog/templates/blog/post/list.html new file mode 100644 index 0000000..8f182df --- /dev/null +++ b/Projects/Testtrack/M1 - Django Learning/mysite/blog/templates/blog/post/list.html @@ -0,0 +1,20 @@ +{% extends "blog/base.html" %} + +{% block title %}Blog Posts{% endblock %} + +{% block content %} +

Blog Posts

+ {% for post in posts %} +

+ + {{ post.title }} + +

+

+ Published on {{ post.published_at }} by {{post.author}} +

+ {{ post.body|truncatewords:30|linebreaks }}

+ {% empty %} +

No posts available.

+ {% endfor %} +{% endblock %} diff --git a/Projects/Testtrack/M1 - Django Learning/mysite/blog/tests.py b/Projects/Testtrack/M1 - Django Learning/mysite/blog/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/Projects/Testtrack/M1 - Django Learning/mysite/blog/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/Projects/Testtrack/M1 - Django Learning/mysite/blog/urls.py b/Projects/Testtrack/M1 - Django Learning/mysite/blog/urls.py new file mode 100644 index 0000000..d560190 --- /dev/null +++ b/Projects/Testtrack/M1 - Django Learning/mysite/blog/urls.py @@ -0,0 +1,9 @@ +from django.urls import path +from . import views + +app_name = 'blog' + +urlpatterns = [ + path('', views.post_list, name='post_list'), + path('/', views.post_detail, name='post_detail') +] diff --git a/Projects/Testtrack/M1 - Django Learning/mysite/blog/views.py b/Projects/Testtrack/M1 - Django Learning/mysite/blog/views.py new file mode 100644 index 0000000..45eb8dc --- /dev/null +++ b/Projects/Testtrack/M1 - Django Learning/mysite/blog/views.py @@ -0,0 +1,25 @@ +from django.shortcuts import get_object_or_404, render +from .models import Post + + +def post_list(request): + posts = Post.published.all() + return render( + request, + 'blog/post/list.html', + {'posts': posts} + ) + + +def post_detail(request, id): + post = get_object_or_404( + Post, + id=id, + status=Post.Status.PUBLISHED + ) + + return render( + request, + 'blog/post/detail.html', + {'post': post} + ) diff --git a/Projects/Testtrack/M1 - Django Learning/mysite/db.sqlite3 b/Projects/Testtrack/M1 - Django Learning/mysite/db.sqlite3 new file mode 100644 index 0000000..221ef97 Binary files /dev/null and b/Projects/Testtrack/M1 - Django Learning/mysite/db.sqlite3 differ diff --git a/Projects/Testtrack/M1 - Django Learning/mysite/manage.py b/Projects/Testtrack/M1 - Django Learning/mysite/manage.py new file mode 100755 index 0000000..a7da667 --- /dev/null +++ b/Projects/Testtrack/M1 - Django Learning/mysite/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/Projects/Testtrack/M1 - Django Learning/mysite/mysite/__init__.py b/Projects/Testtrack/M1 - Django Learning/mysite/mysite/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Projects/Testtrack/M1 - Django Learning/mysite/mysite/asgi.py b/Projects/Testtrack/M1 - Django Learning/mysite/mysite/asgi.py new file mode 100644 index 0000000..4e0d84c --- /dev/null +++ b/Projects/Testtrack/M1 - Django Learning/mysite/mysite/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for mysite project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/5.2/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') + +application = get_asgi_application() diff --git a/Projects/Testtrack/M1 - Django Learning/mysite/mysite/settings.py b/Projects/Testtrack/M1 - Django Learning/mysite/mysite/settings.py new file mode 100644 index 0000000..aec6ed2 --- /dev/null +++ b/Projects/Testtrack/M1 - Django Learning/mysite/mysite/settings.py @@ -0,0 +1,123 @@ +""" +Django settings for mysite project. + +Generated by 'django-admin startproject' using Django 5.2.7. + +For more information on this file, see +https://docs.djangoproject.com/en/5.2/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/5.2/ref/settings/ +""" + +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 https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-s8afcv8t5jbc6znt!wx*ylmq!%-l$!-*6hcqisns_7i201hfuq' + +# 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', + 'blog.apps.BlogConfig', +] + +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 = 'mysite.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'mysite.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/5.2/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/5.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/5.2/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/5.2/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/Projects/Testtrack/M1 - Django Learning/mysite/mysite/urls.py b/Projects/Testtrack/M1 - Django Learning/mysite/mysite/urls.py new file mode 100644 index 0000000..b7eb578 --- /dev/null +++ b/Projects/Testtrack/M1 - Django Learning/mysite/mysite/urls.py @@ -0,0 +1,23 @@ +""" +URL configuration for mysite project. + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/5.2/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('blog/', include('blog.urls', namespace='blog')), +] diff --git a/Projects/Testtrack/M1 - Django Learning/mysite/mysite/wsgi.py b/Projects/Testtrack/M1 - Django Learning/mysite/mysite/wsgi.py new file mode 100644 index 0000000..35e1b4d --- /dev/null +++ b/Projects/Testtrack/M1 - Django Learning/mysite/mysite/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for mysite project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/5.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') + +application = get_wsgi_application() diff --git a/Projects/Testtrack/Testtrack.md b/Projects/Testtrack/Testtrack.md index 0f688a7..8457072 100644 --- a/Projects/Testtrack/Testtrack.md +++ b/Projects/Testtrack/Testtrack.md @@ -8,3 +8,5 @@ Projects: 2. [p911 - Simple eCommerceBackend](./p911%20-%20Simple%20eCommerce%20backend/README.md) 3. [miniS - Algorithms Sandbox](./miniS%20-%20Algoritms%20sandbox/README.md) 4. [fGT40 - Python basics](Projects/Testtrack/fGT40%20-%20Python%20basics/README.md) +5. [Skyline GTR - Text Classification](Projects/Testtrack/Skyline%20GTR%20-%20Text%20Classification/README.md) +5. [M1 - Django Learning](Projects/Testtrack/M1%20-%20Django%20Learning/README.md)