Compare commits

...

3 commits

12 changed files with 92 additions and 0 deletions

View file

@ -35,6 +35,7 @@ INSTALLED_APPS = [
"django.contrib.sessions", "django.contrib.sessions",
"django.contrib.messages", "django.contrib.messages",
"django.contrib.staticfiles", "django.contrib.staticfiles",
"flangr.posts",
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@ -108,3 +109,5 @@ STATIC_URL = "static/"
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
MEDIA_URL = "media/"

View file

@ -15,3 +15,5 @@ DEBUG = True
# SECURITY WARNING: keep the secret key used in production secret! # SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-lt9sfgmvcrjuiz!=l)3f50x&333&p4wi60t1@jdjtrho43ytl+" SECRET_KEY = "django-insecure-lt9sfgmvcrjuiz!=l)3f50x&333&p4wi60t1@jdjtrho43ytl+"
MEDIA_ROOT = "./media"

0
flangr/posts/__init__.py Normal file
View file

3
flangr/posts/admin.py Normal file
View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
flangr/posts/apps.py Normal file
View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class PostsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "flangr.posts"

View file

@ -0,0 +1,32 @@
# Generated by Django 4.0.5 on 2022-07-03 21:35
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = []
operations = [
migrations.CreateModel(
name="Post",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("img", models.ImageField(upload_to="posts/%Y/%m")),
("posted", models.DateTimeField(auto_now_add=True)),
("public", models.BooleanField(default=False)),
("title", models.CharField(max_length=255)),
("body", models.TextField()),
],
),
]

View file

15
flangr/posts/models.py Normal file
View file

@ -0,0 +1,15 @@
from django.db import models
# Create your models here.
class Post(models.Model):
img = models.ImageField(upload_to="posts/%Y/%m")
posted = models.DateTimeField(auto_now_add=True)
public = models.BooleanField(default=False)
title = models.CharField(max_length=255)
body = models.TextField()
def __str__(self):
return f"Post: {self.title} at {self.posted.strftime('%Y-%m-%d %H:%S')}"

Binary file not shown.

After

Width:  |  Height:  |  Size: 320 B

26
flangr/posts/tests.py Normal file
View file

@ -0,0 +1,26 @@
import os
from django.test import TestCase
from django.core.files import File
from .models import Post
# Create your tests here.
class TestPostModelTests(TestCase):
def test_can_create_model(self):
with open(
os.path.join(os.path.dirname(__file__), "test_data", "test_img.png"),
mode="rb",
) as f:
try:
p = Post.objects.create(
img=File(f, "somefile.png"),
title="Foobar",
body="Some file",
)
self.assertIn("Foobar", str(p))
finally:
p.img.delete()

3
flangr/posts/views.py Normal file
View file

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

View file

@ -0,0 +1,2 @@
pillow
django