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

Add blog section with Markdown support #2804

Open
wants to merge 1 commit into
base: main
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions blog/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib import admin
from .models import Post

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'author', 'created_at')
prepopulated_fields = {'slug': ('title',)}
17 changes: 17 additions & 0 deletions blog/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse

class Post(models.Model):
title = models.CharField(max_length=200)
slug = models.SlugField(unique=True)
author = models.ForeignKey(User, on_delete=models.CASCADE)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

def __str__(self):
return self.title

def get_absolute_url(self):
return reverse('post_detail', kwargs={'slug': self.slug})
82 changes: 82 additions & 0 deletions blog/static/blog/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* Blog Section Styles */

.blog-container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}

.blog-title {
font-size: 2em;
font-weight: bold;
margin-bottom: 10px;
}

.blog-author {
font-size: 1em;
color: #555;
margin-bottom: 20px;
}

.blog-content {
font-size: 1.2em;
line-height: 1.6;
}

.blog-content img {
max-width: 100%;
height: auto;
}

.blog-content pre {
background: #f4f4f4;
padding: 10px;
border-radius: 5px;
overflow-x: auto;
}

.blog-content code {
background: #f4f4f4;
padding: 2px 4px;
border-radius: 3px;
}

.blog-content blockquote {
border-left: 4px solid #ccc;
padding-left: 10px;
color: #666;
margin: 20px 0;
}

.blog-content ul,
.blog-content ol {
margin: 20px 0;
padding-left: 40px;
}

.blog-content h1,
.blog-content h2,
.blog-content h3,
.blog-content h4,
.blog-content h5,
.blog-content h6 {
margin-top: 20px;
margin-bottom: 10px;
}

.blog-content table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
}

.blog-content table th,
.blog-content table td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}

.blog-content table th {
background: #f4f4f4;
}
3 changes: 3 additions & 0 deletions blog/static/blog/js/editor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
document.addEventListener("DOMContentLoaded", function() {
var easyMDE = new EasyMDE({ element: document.getElementById('id_content') });
});
11 changes: 11 additions & 0 deletions blog/templates/blog/post_confirm_delete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends 'base.html' %}

{% block content %}
<h1>Confirm Delete</h1>
<p>Are you sure you want to delete the post "{{ post.title }}"?</p>
<form method="post">
{% csrf_token %}
<button type="submit">Yes, delete</button>
<a href="{% url 'post_detail' slug=post.slug %}">Cancel</a>
</form>
{% endblock %}
11 changes: 11 additions & 0 deletions blog/templates/blog/post_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends 'base.html' %}

{% block content %}
<article>
<h1>{{ post.title }}</h1>
<p>By {{ post.author }} on {{ post.created_at }}</p>
<div>
{{ post.content|safe }}
</div>
</article>
{% endblock %}
16 changes: 16 additions & 0 deletions blog/templates/blog/post_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends 'base.html' %}

{% block content %}
<h1>{% if form.instance.pk %}Edit{% else %}New{% endif %} Post</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save</button>
</form>

<link rel="stylesheet" href="https://unpkg.com/easymde/dist/easymde.min.css">
<script src="https://unpkg.com/easymde/dist/easymde.min.js"></script>
<script>
var easyMDE = new EasyMDE({ element: document.getElementById('id_content') });
</script>
{% endblock %}
14 changes: 14 additions & 0 deletions blog/templates/blog/post_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends 'base.html' %}

{% block content %}
<h1>Blog Posts</h1>
<ul>
{% for post in posts %}
<li>
<a href="{% url 'post_detail' slug=post.slug %}">{{ post.title }}</a> by {{ post.author }}
</li>
{% empty %}
<p>No posts available.</p>
{% endfor %}
</ul>
{% endblock %}
10 changes: 10 additions & 0 deletions blog/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.urls import path
from . import views

urlpatterns = [
path('', views.PostListView.as_view(), name='post_list'),
path('post/<slug:slug>/', views.PostDetailView.as_view(), name='post_detail'),
path('post/new/', views.PostCreateView.as_view(), name='post_create'),
path('post/<slug:slug>/edit/', views.PostUpdateView.as_view(), name='post_update'),
path('post/<slug:slug>/delete/', views.PostDeleteView.as_view(), name='post_delete'),
]
50 changes: 50 additions & 0 deletions blog/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from django.views import generic
from .models import Post
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
import markdown

class PostListView(generic.ListView):
model = Post
template_name = 'blog/post_list.html'
context_object_name = 'posts'
paginate_by = 5

class PostDetailView(generic.DetailView):
model = Post
template_name = 'blog/post_detail.html'

def get_object(self):
post = super().get_object()
post.content = markdown.markdown(post.content)
return post

class PostCreateView(LoginRequiredMixin, generic.CreateView):
model = Post
fields = ['title', 'content']
template_name = 'blog/post_form.html'

def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)

class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, generic.UpdateView):
model = Post
fields = ['title', 'content']
template_name = 'blog/post_form.html'

def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)

def test_func(self):
post = self.get_object()
return self.request.user == post.author

class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, generic.DeleteView):
model = Post
template_name = 'blog/post_confirm_delete.html'
success_url = '/'

def test_func(self):
post = self.get_object()
return self.request.user == post.author
1 change: 1 addition & 0 deletions blt/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"captcha",
"dj_rest_auth",
"dj_rest_auth.registration",
"blog",
)


Expand Down
1 change: 1 addition & 0 deletions blt/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@
path("sizzle/", website.views.sizzle, name="sizzle"),
path("sizzle-docs/", website.views.sizzle_docs, name="sizzle-docs"),
path("api/timelogsreport/", website.views.TimeLogListAPIView, name="timelogsreport"),
path('blog/', include('blog.urls')),
]

if settings.DEBUG:
Expand Down
Loading