First initial commit for test Django website

This commit is contained in:
Alexander Schulz
2024-08-30 16:35:36 +02:00
commit 014b3995ae
169 changed files with 32693 additions and 0 deletions

0
post/__init__.py Normal file
View File

4
post/admin.py Normal file
View File

@ -0,0 +1,4 @@
from django.contrib import admin
from .models import Post
# Register your models here.
admin.site.register(Post)

6
post/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class PostConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'post'

8
post/forms.py Normal file
View File

@ -0,0 +1,8 @@
from django import forms
from . import models
class CreatePost(forms.ModelForm):
class Meta:
model = models.Post
fields = ['title' , 'body' , 'slug' , 'banner']

View File

@ -0,0 +1,24 @@
# Generated by Django 5.1 on 2024-08-29 22:45
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')),
('title', models.CharField(max_length=75)),
('body', models.TextField()),
('slug', models.SlugField()),
('date', models.DateTimeField(auto_now_add=True)),
],
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 5.1 on 2024-08-29 23:39
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('post', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='post',
name='banner',
field=models.ImageField(blank=True, default='fallback.png', upload_to=''),
),
]

View File

15
post/models.py Normal file
View File

@ -0,0 +1,15 @@
from django.db import models
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=75)
body = models.TextField()
slug = models.SlugField()
date = models.DateTimeField(auto_now_add=True)
banner = models.ImageField(default='fallback.png', blank=True)
def __str__(self):
return self.title

View File

@ -0,0 +1,25 @@
{% extends 'layout.html' %}
{% block title %}
Posts
{% endblock %}
{% block content %}
<section>
<h1>Posts</h1>
{% for post in posts %}
<article class="post">
<h2>
<a href="{% url 'post:page' slug=post.slug %}">
{{ post.title }}
</a>
</h2>
<p>{{ post.date }}</p>
<p>{{ post.body }}</p>
</article>
{% endfor %}
</section>
{% endblock %}

View File

@ -0,0 +1,16 @@
{% extends 'layout.html' %}
{% block title %}
New Post
{% endblock %}
{% block content %}
<section>
<h1>New Post</h1>
<form class="form-with-validation" action="{% url 'post:new-post' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form }}
<button class="form-submit">Add Post</button>
</form>
</section>
{% endblock %}

View File

@ -0,0 +1,18 @@
{% extends 'layout.html' %}
{% block title %}
{{ post.title }}
{% endblock %}
{% block content %}
<section>
<img
class="banner"
src="{{ post.banner.url }}"
alt= {{ post.title}} />
<h1>{{ post.title }}</h1>
<p>{{ post.date }}</p>
<p>{{ post.body }}</p>
</section>
{% endblock %}

3
post/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

11
post/urls.py Normal file
View File

@ -0,0 +1,11 @@
from django.urls import path
from . import views
app_name = 'post'
urlpatterns = [
path('', views.post_list, name="list"),
path('new-post/', views.post_new, name="new-post"),
path('<slug:slug>', views.post_page, name="page"),
]

20
post/views.py Normal file
View File

@ -0,0 +1,20 @@
from django.shortcuts import render
from .models import Post
from django.contrib.auth.decorators import login_required
from . import forms
# Create your views here.
def post_list(request):
posts = Post.objects.all().order_by('-date')
return render(request, 'post/post_list.html', {'posts': posts})
def post_page(request, slug):
post = Post.objects.get(slug=slug)
return render(request, 'post/post_page.html', {'post': post})
@login_required(login_url="/users/login/")
def post_new(request):
form = forms.CreatePost()
return render(request, 'post/post_new.html', {'form': form})