Add ckeditor to post creation page

This commit is contained in:
Piotr Szkudlarek 2024-01-07 01:10:31 +01:00
parent 4bba2f5b75
commit f477188521
11 changed files with 63 additions and 14 deletions

View File

@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2024-01-06 23:25
import ckeditor.fields
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("Community", "0003_comment_date_added"),
]
operations = [
migrations.AlterField(
model_name="post",
name="content",
field=ckeditor.fields.RichTextField(blank=True, null=True),
),
]

View File

@ -0,0 +1,22 @@
# Generated by Django 4.2.7 on 2024-01-07 00:00
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("Community", "0004_alter_post_content"),
]
operations = [
migrations.AlterField(
model_name="comment",
name="date_added",
field=models.DateTimeField(auto_now_add=True),
),
migrations.AlterField(
model_name="post",
name="date_pub",
field=models.DateTimeField(auto_now_add=True),
),
]

View File

@ -1,14 +1,15 @@
from django.db import models from django.db import models
from django.contrib.auth.models import User from django.contrib.auth.models import User
from ckeditor.fields import RichTextField
# Create your models here. # Create your models here.
class Post(models.Model): class Post(models.Model):
title = models.CharField(max_length=500) title = models.CharField(max_length=500)
content = models.TextField() content = RichTextField(null=True, blank=True)
author = models.ForeignKey(User, on_delete=models.CASCADE, null=False) author = models.ForeignKey(User, on_delete=models.CASCADE, null=False)
date_pub = models.DateField(auto_now_add=True) date_pub = models.DateTimeField(auto_now_add=True)
@property @property
def sorted_comments(self): def sorted_comments(self):
@ -19,4 +20,4 @@ class Comment(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE, null=False) author = models.ForeignKey(User, on_delete=models.CASCADE, null=False)
content = models.TextField() content = models.TextField()
post = models.ForeignKey(Post, on_delete=models.CASCADE) post = models.ForeignKey(Post, on_delete=models.CASCADE)
date_added = models.DateField(auto_now_add=True) date_added = models.DateTimeField(auto_now_add=True)

View File

@ -11,7 +11,7 @@ from .froms import PostForm, CommentForm
class ListPosts(ListView): class ListPosts(ListView):
model = Post model = Post
template_name = "list_posts.html" template_name = "list_posts.html"
paginate_by = 3 paginate_by = 4
def get_queryset(self) -> QuerySet[Any]: def get_queryset(self) -> QuerySet[Any]:
queryset = Post.objects.all().order_by("-date_pub") queryset = Post.objects.all().order_by("-date_pub")

Binary file not shown.

View File

@ -5,13 +5,15 @@
align-items: center; align-items: center;
} }
#add_post p{ #add_post input{
display: flex; margin-bottom: 10px;
flex-direction: row;
justify-content: space-between;
align-items: center;
} }
#add_post label{
margin-bottom: 10px;
font-size: 20px;
font-weight: bold;
}
#submit{ #submit{
background-color: chartreuse; background-color: chartreuse;
@ -22,4 +24,5 @@
color:black; color:black;
font-family: Arial; font-family: Arial;
text-align: center; text-align: center;
margin-top: 10px;
} }

View File

@ -47,4 +47,5 @@ ul{
box-shadow: 0 0 0 2px black; box-shadow: 0 0 0 2px black;
font-size: 20px; font-size: 20px;
font-family: Arial; font-family: Arial;
margin-bottom:10px;
} }

View File

@ -12,7 +12,7 @@
margin-top:10px; margin-top:10px;
flex-direction: column; flex-direction: column;
justify-content:center; justify-content:center;
align-items: flex-start; align-items: stretch;
border: 3px solid lightgray; border: 3px solid lightgray;
width: 80%; width: 80%;
padding: 15px; padding: 15px;
@ -30,6 +30,7 @@
font-size: 40px; font-size: 40px;
color: black; color: black;
font-weight: bold; font-weight: bold;
align-self: flex-start;
} }
#content{ #content{

View File

@ -6,7 +6,8 @@
{%block content%} {%block content%}
<form method="POST" id="add_post"> <form method="POST" id="add_post">
{% csrf_token %} {% csrf_token %}
{{ form.as_p }} {{form}}
{{form.media}}
<input type="submit" value="Add post" id="submit"> <input type="submit" value="Add post" id="submit">
</form> </form>
{%endblock content%} {%endblock content%}

View File

@ -12,7 +12,7 @@
<div class="history_details"> <div class="history_details">
<img src="{{detection.images.first.image.url}}" alt="upload_image" style="width:125px; height:125px;"> <img src="{{detection.images.first.image.url}}" alt="upload_image" style="width:125px; height:125px;">
<div class="date_author"> <div class="date_author">
<p class="date">{{detection.date_predicted.date}}</p> <p class="date">{{detection.date_predicted}}</p>
<p class="author">{{detection.owner}}</p> <p class="author">{{detection.owner}}</p>
<p class="num_images">Images uploaded: {{detection.images.all.count}}</p> <p class="num_images">Images uploaded: {{detection.images.all.count}}</p>
</div> </div>

View File

@ -8,13 +8,14 @@
<div class="post"> <div class="post">
<div class="title-content"> <div class="title-content">
<p id="title">{{object.title}}</p> <p id="title">{{object.title}}</p>
<p id="content">{{object.content}}</p> <p id="content">{{object.content|safe}}</p>
</div> </div>
<div class="author"> <div class="author">
<p id="author">{{object.author}}</p> <p id="author">{{object.author}}</p>
</div> </div>
</div> </div>
<p id="comment-label">Comments:</p> <p id="comment-label">Comments:</p>
{%if user.is_authenticated%}
<div id="add_comment"> <div id="add_comment">
<form method="POST" id="comment_form"> <form method="POST" id="comment_form">
{%csrf_token%} {%csrf_token%}
@ -22,6 +23,7 @@
<input type="submit" id="submit" value="Add comment"> <input type="submit" id="submit" value="Add comment">
</form> </form>
</div> </div>
{%endif%}
<div class="comments"> <div class="comments">
{%for comment in object.sorted_comments%} {%for comment in object.sorted_comments%}
<div class="single-comment"> <div class="single-comment">