Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish Introduction and Django Admin #19

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
Binary file added blango/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file added blango/__pycache__/settings.cpython-312.pyc
Binary file not shown.
Binary file added blango/__pycache__/urls.cpython-312.pyc
Binary file not shown.
Binary file added blango/__pycache__/wsgi.cpython-312.pyc
Binary file not shown.
12 changes: 9 additions & 3 deletions blango/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

ALLOWED_HOSTS = ['*']

# Bootstrap
CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"
CRISPY_TEMPLATE_PACK = "bootstrap5"
# Application definition

INSTALLED_APPS = [
Expand All @@ -37,6 +39,9 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
"crispy_forms",
"crispy_bootstrap5",
]

MIDDLEWARE = [
Expand All @@ -54,7 +59,7 @@
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [BASE_DIR / 'blango/templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
Expand All @@ -67,6 +72,7 @@
},
]


WSGI_APPLICATION = 'blango.wsgi.application'


Expand Down
7 changes: 6 additions & 1 deletion blango/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from blog import views
from django.contrib import admin
from django.urls import path
from django.urls import path, include
import blog.views

urlpatterns = [
# path('blog/', include('blog.index.html')),
path('admin/', admin.site.urls),
path('', views.index),
path("post/<slug>/", blog.views.post_detail, name="blog-post-detail"),
]
Empty file added blog/__init__.py
Empty file.
Binary file added blog/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file added blog/__pycache__/admin.cpython-312.pyc
Binary file not shown.
Binary file added blog/__pycache__/apps.cpython-312.pyc
Binary file not shown.
Binary file added blog/__pycache__/forms.cpython-312.pyc
Binary file not shown.
Binary file added blog/__pycache__/models.cpython-312.pyc
Binary file not shown.
Binary file added blog/__pycache__/views.cpython-312.pyc
Binary file not shown.
13 changes: 13 additions & 0 deletions blog/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.contrib import admin

from blog.models import Tag, Post, Comment

# Register your models here.

class PostAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("title",)}


admin.site.register(Post, PostAdmin)
admin.site.register(Tag)
admin.site.register(Comment)
6 changes: 6 additions & 0 deletions 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"
15 changes: 15 additions & 0 deletions blog/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit

from blog.models import Comment

class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ["content"]

def __init__(self, *args, **kwargs):
super(CommentForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.add_input(Submit('submit', 'Submit'))
61 changes: 61 additions & 0 deletions blog/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Generated by Django 5.1 on 2024-08-23 09:00

import django.db.models.deletion
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="Tag",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("value", models.TextField(max_length=100)),
],
),
migrations.CreateModel(
name="Post",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("created_at", models.DateTimeField(auto_now_add=True)),
("modified_at", models.DateTimeField(auto_now=True)),
("published_at", models.DateTimeField(blank=True, null=True)),
("title", models.TextField(max_length=100)),
("slug", models.SlugField()),
("summary", models.TextField(max_length=500)),
("content", models.TextField()),
(
"author",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
to=settings.AUTH_USER_MODEL,
),
),
("tags", models.ManyToManyField(related_name="posts", to="blog.tag")),
],
),
]
47 changes: 47 additions & 0 deletions blog/migrations/0002_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Generated by Django 5.1 on 2024-08-23 17:35

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("blog", "0001_initial"),
("contenttypes", "0002_remove_content_type_name"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="Comment",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("content", models.TextField()),
("object_id", models.PositiveIntegerField()),
(
"content_type",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="contenttypes.contenttype",
),
),
(
"creator",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
),
]
16 changes: 16 additions & 0 deletions blog/migrations/0003_delete_post.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Generated by Django 5.1 on 2024-08-23 19:11

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("blog", "0002_comment"),
]

operations = [
migrations.DeleteModel(
name="Post",
),
]
45 changes: 45 additions & 0 deletions blog/migrations/0004_post.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Generated by Django 5.1 on 2024-08-23 19:12

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("blog", "0003_delete_post"),
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",
),
),
("created_at", models.DateTimeField(auto_now_add=True)),
("modified_at", models.DateTimeField(auto_now=True)),
("published_at", models.DateTimeField(blank=True, null=True)),
("title", models.TextField(max_length=100)),
("slug", models.SlugField()),
("summary", models.TextField(max_length=500)),
("content", models.TextField()),
(
"author",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
to=settings.AUTH_USER_MODEL,
),
),
("tags", models.ManyToManyField(related_name="posts", to="blog.tag")),
],
),
]
27 changes: 27 additions & 0 deletions blog/migrations/0005_comment_created_at_comment_modified_at.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 5.1 on 2024-08-23 19:20

import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("blog", "0004_post"),
]

operations = [
migrations.AddField(
model_name="comment",
name="created_at",
field=models.DateTimeField(
auto_now_add=True, default=django.utils.timezone.now
),
preserve_default=False,
),
migrations.AddField(
model_name="comment",
name="modified_at",
field=models.DateTimeField(auto_now=True),
),
]
Empty file added blog/migrations/__init__.py
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added blog/migrations/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
40 changes: 40 additions & 0 deletions blog/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from django.db import models
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericRelation
# Create your models here.
class Tag(models.Model):
value = models.TextField(max_length=100)

def __str__(self):
return self.value

class Comment(models.Model):
creator = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
content = models.TextField()
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey("content_type", "object_id")
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)

class Post(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT)
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
published_at = models.DateTimeField(blank=True, null=True)
title = models.TextField(max_length=100)
slug = models.SlugField()
summary = models.TextField(max_length=500)
content = models.TextField()
tags = models.ManyToManyField(Tag, related_name="posts")
comments = GenericRelation(Comment)

def __str__(self):
return self.title





19 changes: 19 additions & 0 deletions blog/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<body>

<div class="container bg-dark text-light">
{% block content %}

{% endblock %}
</div>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</body>
</html>
18 changes: 18 additions & 0 deletions blog/templates/blog/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends "base.html" %}
{% load blog_extras %}
{% block content %}
<h2>Blog Posts</h2>
{% for post in posts %}
{% row "border-bottom" %}
<div class="col">
<h3>{{ post.title }}</h3>
{% include "blog/post-byline.html" %}
<p>{{ post.summary }}</p>
<p>
({{ post.content|wordcount }} words)
<a href="{% url "blog-post-detail" post.slug %}">Read More</a>
</p>
</div>
{% endrow %}
{% endfor %}
{% endblock %}
3 changes: 3 additions & 0 deletions blog/templates/blog/post-byline.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

{% load blog_extras %}
<small>By {{ post.author|author_details:request.user }} on {{ post.published_at|date:"M, d Y" }}</small>
Loading