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 %} + + +
+ + ++ 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 %} ++ 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('