Skip to content
This repository was archived by the owner on Mar 27, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Atlas/Engineering/Programming/Python/Django/Manage commands.md
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions Projects/Testtrack/M1 - Django Learning/README.md
Original file line number Diff line number Diff line change
@@ -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/)
14 changes: 14 additions & 0 deletions Projects/Testtrack/M1 - Django Learning/mysite/blog/admin.py
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions Projects/Testtrack/M1 - Django Learning/mysite/blog/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class BlogConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'blog'
Original file line number Diff line number Diff line change
@@ -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')],
},
),
]
41 changes: 41 additions & 0 deletions Projects/Testtrack/M1 - Django Learning/mysite/blog/models.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
</head>
<body>
<article id="content">
{% block content %}{% endblock %}
</article>
<aside id="sidebar">
<h2>My blog</h2>
<p> Hello dude!</p>
</aside>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "blog/base.html" %}

{% block title %}{{ post.title }}{% endblock %}

{% block content %}
<h1>{{ post.title }}</h1>
<p class="date">
Published on {{ post.published_at }} by {{post.author}}
</p>
{{ post.body|linebreaks }}</p>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends "blog/base.html" %}

{% block title %}Blog Posts{% endblock %}

{% block content %}
<h1>Blog Posts</h1>
{% for post in posts %}
<h2>
<a href="{% url 'blog:post_detail' post.id %}">
{{ post.title }}
</a>
</h2>
<p class="date">
Published on {{ post.published_at }} by {{post.author}}
</p>
{{ post.body|truncatewords:30|linebreaks }}</p>
{% empty %}
<p>No posts available.</p>
{% endfor %}
{% endblock %}
3 changes: 3 additions & 0 deletions Projects/Testtrack/M1 - Django Learning/mysite/blog/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
9 changes: 9 additions & 0 deletions Projects/Testtrack/M1 - Django Learning/mysite/blog/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import path
from . import views

app_name = 'blog'

urlpatterns = [
path('', views.post_list, name='post_list'),
path('<int:id>/', views.post_detail, name='post_detail')
]
25 changes: 25 additions & 0 deletions Projects/Testtrack/M1 - Django Learning/mysite/blog/views.py
Original file line number Diff line number Diff line change
@@ -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}
)
Binary file not shown.
22 changes: 22 additions & 0 deletions Projects/Testtrack/M1 - Django Learning/mysite/manage.py
Original file line number Diff line number Diff line change
@@ -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()
16 changes: 16 additions & 0 deletions Projects/Testtrack/M1 - Django Learning/mysite/mysite/asgi.py
Original file line number Diff line number Diff line change
@@ -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()
Loading
Loading