diff --git a/Module02/mysite/blog/__init__.py b/Module02/mysite/blog/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Module02/mysite/blog/__pycache__/__init__.cpython-311.pyc b/Module02/mysite/blog/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 0000000..58f6ad8 Binary files /dev/null and b/Module02/mysite/blog/__pycache__/__init__.cpython-311.pyc differ diff --git a/Module02/mysite/blog/__pycache__/admin.cpython-311.pyc b/Module02/mysite/blog/__pycache__/admin.cpython-311.pyc new file mode 100644 index 0000000..5102c49 Binary files /dev/null and b/Module02/mysite/blog/__pycache__/admin.cpython-311.pyc differ diff --git a/Module02/mysite/blog/__pycache__/apps.cpython-311.pyc b/Module02/mysite/blog/__pycache__/apps.cpython-311.pyc new file mode 100644 index 0000000..a8cfd22 Binary files /dev/null and b/Module02/mysite/blog/__pycache__/apps.cpython-311.pyc differ diff --git a/Module02/mysite/blog/__pycache__/models.cpython-311.pyc b/Module02/mysite/blog/__pycache__/models.cpython-311.pyc new file mode 100644 index 0000000..da344cb Binary files /dev/null and b/Module02/mysite/blog/__pycache__/models.cpython-311.pyc differ diff --git a/Module02/mysite/blog/__pycache__/urls.cpython-311.pyc b/Module02/mysite/blog/__pycache__/urls.cpython-311.pyc new file mode 100644 index 0000000..7fd192e Binary files /dev/null and b/Module02/mysite/blog/__pycache__/urls.cpython-311.pyc differ diff --git a/Module02/mysite/blog/__pycache__/views.cpython-311.pyc b/Module02/mysite/blog/__pycache__/views.cpython-311.pyc new file mode 100644 index 0000000..4a30d2a Binary files /dev/null and b/Module02/mysite/blog/__pycache__/views.cpython-311.pyc differ diff --git a/Module02/mysite/blog/admin.py b/Module02/mysite/blog/admin.py new file mode 100644 index 0000000..1e1fd05 --- /dev/null +++ b/Module02/mysite/blog/admin.py @@ -0,0 +1,13 @@ +from django.contrib import admin +from .models import Post + + +@admin.register(Post) +class PostAdmin(admin.ModelAdmin): + list_display = ['title', 'slug', 'author', 'publish', 'status'] + list_filter = ['status', 'created', 'publish', 'author'] + search_fields = ['title', 'body'] + prepopulated_fields = {'slug': ('title',)} + raw_id_fields = ['author'] + date_hierarchy = 'publish' + ordering = ['status', 'publish'] diff --git a/Module02/mysite/blog/apps.py b/Module02/mysite/blog/apps.py new file mode 100644 index 0000000..94788a5 --- /dev/null +++ b/Module02/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/Module02/mysite/blog/migrations/0001_initial.py b/Module02/mysite/blog/migrations/0001_initial.py new file mode 100644 index 0000000..6e137ca --- /dev/null +++ b/Module02/mysite/blog/migrations/0001_initial.py @@ -0,0 +1,36 @@ +# Generated by Django 4.2.4 on 2023-08-20 07:26 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone + + +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()), + ('publish', models.DateTimeField(default=django.utils.timezone.now)), + ('created', models.DateTimeField(auto_now_add=True)), + ('updated', 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': ['-publish'], + 'indexes': [models.Index(fields=['-publish'], name='blog_post_publish_bb7600_idx')], + }, + ), + ] diff --git a/Module02/mysite/blog/migrations/__init__.py b/Module02/mysite/blog/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Module02/mysite/blog/migrations/__pycache__/0001_initial.cpython-311.pyc b/Module02/mysite/blog/migrations/__pycache__/0001_initial.cpython-311.pyc new file mode 100644 index 0000000..e3305c3 Binary files /dev/null and b/Module02/mysite/blog/migrations/__pycache__/0001_initial.cpython-311.pyc differ diff --git a/Module02/mysite/blog/migrations/__pycache__/__init__.cpython-311.pyc b/Module02/mysite/blog/migrations/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 0000000..fbe478e Binary files /dev/null and b/Module02/mysite/blog/migrations/__pycache__/__init__.cpython-311.pyc differ diff --git a/Module02/mysite/blog/models.py b/Module02/mysite/blog/models.py new file mode 100644 index 0000000..abaaec2 --- /dev/null +++ b/Module02/mysite/blog/models.py @@ -0,0 +1,27 @@ +from django.db import models +from django.utils import timezone +from django.contrib.auth.models import User + +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(User, on_delete=models.CASCADE, related_name='blog_posts') + body = models.TextField() + publish = models.DateTimeField(default=timezone.now) + created = models.DateTimeField(auto_now_add=True) + updated = models.DateTimeField(auto_now=True) + status = models.CharField(max_length=2, choices=Status.choices, default=Status.DRAFT) + + class Meta: + ordering = ['-publish'] + indexes = [ + models.Index(fields=['-publish']), + ] + + def __str__(self): + return self.title diff --git a/Module02/mysite/blog/static/css/blog.css b/Module02/mysite/blog/static/css/blog.css new file mode 100644 index 0000000..9047395 --- /dev/null +++ b/Module02/mysite/blog/static/css/blog.css @@ -0,0 +1,92 @@ +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; +} + +/* 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; +} \ No newline at end of file diff --git a/Module02/mysite/blog/templates/blog/base.html b/Module02/mysite/blog/templates/blog/base.html new file mode 100644 index 0000000..d2a462f --- /dev/null +++ b/Module02/mysite/blog/templates/blog/base.html @@ -0,0 +1,19 @@ +{% load static %} + + + + + {% block title %}{% endblock %} + + + +
+ {% block content %} + {% endblock %} +
+ + + \ No newline at end of file diff --git a/Module02/mysite/blog/templates/blog/post/detail.html b/Module02/mysite/blog/templates/blog/post/detail.html new file mode 100644 index 0000000..5f7f8d8 --- /dev/null +++ b/Module02/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 {{ post.publish }} by {{ post.author }} +

+ {{ post.body|linebreaks }} +{% endblock %} diff --git a/Module02/mysite/blog/templates/blog/post/list.html b/Module02/mysite/blog/templates/blog/post/list.html new file mode 100644 index 0000000..2afbce1 --- /dev/null +++ b/Module02/mysite/blog/templates/blog/post/list.html @@ -0,0 +1,19 @@ +{% extends "blog/base.html" %} + +{% block title %}My Blog{% endblock %} + +{% block content %} +

My Blog

+ {% for post in posts %} +

+ + {{ post.title }} + +

+

+ Published {{ post.publish }} by {{ post.author }} +

+ {{ post.body|truncatewords:30|linebreaks }} +
+ {% endfor %} +{% endblock %} diff --git a/Module02/mysite/blog/tests.py b/Module02/mysite/blog/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/Module02/mysite/blog/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/Module02/mysite/blog/urls.py b/Module02/mysite/blog/urls.py new file mode 100644 index 0000000..d73bb7b --- /dev/null +++ b/Module02/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'), +] \ No newline at end of file diff --git a/Module02/mysite/blog/views.py b/Module02/mysite/blog/views.py new file mode 100644 index 0000000..976f23a --- /dev/null +++ b/Module02/mysite/blog/views.py @@ -0,0 +1,10 @@ +from django.shortcuts import render, get_object_or_404 +from .models import Post + +def post_list(request): + posts = Post.objects.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}) \ No newline at end of file diff --git a/Module02/mysite/db.sqlite3 b/Module02/mysite/db.sqlite3 new file mode 100644 index 0000000..5b5b283 Binary files /dev/null and b/Module02/mysite/db.sqlite3 differ diff --git a/Module02/mysite/manage.py b/Module02/mysite/manage.py new file mode 100644 index 0000000..a7da667 --- /dev/null +++ b/Module02/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/Module02/mysite/mysite/__init__.py b/Module02/mysite/mysite/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Module02/mysite/mysite/__pycache__/__init__.cpython-311.pyc b/Module02/mysite/mysite/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 0000000..c049874 Binary files /dev/null and b/Module02/mysite/mysite/__pycache__/__init__.cpython-311.pyc differ diff --git a/Module02/mysite/mysite/__pycache__/settings.cpython-311.pyc b/Module02/mysite/mysite/__pycache__/settings.cpython-311.pyc new file mode 100644 index 0000000..3338789 Binary files /dev/null and b/Module02/mysite/mysite/__pycache__/settings.cpython-311.pyc differ diff --git a/Module02/mysite/mysite/__pycache__/urls.cpython-311.pyc b/Module02/mysite/mysite/__pycache__/urls.cpython-311.pyc new file mode 100644 index 0000000..35ca919 Binary files /dev/null and b/Module02/mysite/mysite/__pycache__/urls.cpython-311.pyc differ diff --git a/Module02/mysite/mysite/__pycache__/wsgi.cpython-311.pyc b/Module02/mysite/mysite/__pycache__/wsgi.cpython-311.pyc new file mode 100644 index 0000000..f6211a1 Binary files /dev/null and b/Module02/mysite/mysite/__pycache__/wsgi.cpython-311.pyc differ diff --git a/Module02/mysite/mysite/asgi.py b/Module02/mysite/mysite/asgi.py new file mode 100644 index 0000000..cce50dc --- /dev/null +++ b/Module02/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/4.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/Module02/mysite/mysite/settings.py b/Module02/mysite/mysite/settings.py new file mode 100644 index 0000000..96ed134 --- /dev/null +++ b/Module02/mysite/mysite/settings.py @@ -0,0 +1,124 @@ +""" +Django settings for mysite project. + +Generated by 'django-admin startproject' using Django 4.2.4. + +For more information on this file, see +https://docs.djangoproject.com/en/4.2/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.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/4.2/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-k4tjiq0w@8b!c_q139usr-mfq)3vvj^d2n%b5pek9zybd%5l(!' + +# 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.debug', + '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/4.2/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.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/4.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/4.2/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/Module02/mysite/mysite/urls.py b/Module02/mysite/mysite/urls.py new file mode 100644 index 0000000..571ba12 --- /dev/null +++ b/Module02/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/4.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/Module02/mysite/mysite/wsgi.py b/Module02/mysite/mysite/wsgi.py new file mode 100644 index 0000000..4ce2cc5 --- /dev/null +++ b/Module02/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/4.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/Module02/requirements.txt b/Module02/requirements.txt new file mode 100644 index 0000000..43e2e00 --- /dev/null +++ b/Module02/requirements.txt @@ -0,0 +1,4 @@ +asgiref==3.7.2 +Django==4.2.4 +sqlparse==0.4.4 +tzdata==2023.3