'Can I write posts for a personal Django blog website in static html files?
I'm pretty new to Django and have been experimenting making my own personal blog.
General question
I get that I can specify my blog post structure in models.py and use the admin site to write my blogs. However, what if I want to write my blogs in raw HTML files stored in, e.g. my-website/blog/posts/<list-of-html-blogs> and then have those files uploaded into the data base and shown on the website. Is there are a standard way of doing this?
My initial approach is to create a Python function that reads in the HTML files as strings and uploads those files into the database manually, i.e. using something like blog.models.Post(title=my_title, content=html_as_string). Whenever I'd write a new blog, I'd then call this function to upload a specific html file. However, I have found very little online about whether this is the correct approach.
Django blog overview
If helpful, I'm showing below what my blog's models.py and views.py currently looks like.
models.py
from django.db import models
STATUS = (
(0, "draft"),
(1, "published")
)
class Post(models.Model):
title = models.CharField(max_length=200, unique=True)
slug = models.SlugField(max_length=200, unique=True, null=True)
date = models.DateTimeField()
content = models.TextField(blank=True)
status = models.IntegerField(choices=STATUS, default=0)
class Meta(object):
ordering = ["-date"]
def __str__(self):
return self.title
views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.views import generic
from .models import Post
class RecentPosts(generic.ListView):
model = Post
context_object_name = "postlist"
queryset = Post.objects.all()[:5]
template_name = "blog/index.html"
class SinglePost(generic.DetailView):
model = Post
template_name = "blog/post.html"
class AllPosts(generic.ListView):
model = Post
context_object_name = "posts"
queryset = Post.objects.all()
template_name = "blog/all-posts.html"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
