First initial commit for test Django website
This commit is contained in:
0
post/__init__.py
Normal file
0
post/__init__.py
Normal file
4
post/admin.py
Normal file
4
post/admin.py
Normal 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
6
post/apps.py
Normal 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
8
post/forms.py
Normal 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']
|
||||
|
||||
24
post/migrations/0001_initial.py
Normal file
24
post/migrations/0001_initial.py
Normal 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)),
|
||||
],
|
||||
),
|
||||
]
|
||||
18
post/migrations/0002_post_banner.py
Normal file
18
post/migrations/0002_post_banner.py
Normal 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=''),
|
||||
),
|
||||
]
|
||||
0
post/migrations/__init__.py
Normal file
0
post/migrations/__init__.py
Normal file
15
post/models.py
Normal file
15
post/models.py
Normal 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
|
||||
|
||||
|
||||
|
||||
25
post/templates/post/post_list.html
Normal file
25
post/templates/post/post_list.html
Normal 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 %}
|
||||
16
post/templates/post/post_new.html
Normal file
16
post/templates/post/post_new.html
Normal 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 %}
|
||||
18
post/templates/post/post_page.html
Normal file
18
post/templates/post/post_page.html
Normal 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
3
post/tests.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
11
post/urls.py
Normal file
11
post/urls.py
Normal 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
20
post/views.py
Normal 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})
|
||||
Reference in New Issue
Block a user