如何使用django加载博客网站的文章页面



我想创建一个博客网站。我已经创建了该网站的主页,在我的博客网站上有4篇文章。我想通过点击打开一篇文章,它会将我重定向到唯一的文章页面。每个文章页面都很少有图片、标题和换行符。我将如何使用django将这些上传到我的博客模型中?文章页面示例。。。查看文章页面的图片

有几种方法可以做到这一点,但我建议Django 使用CKEditor

对于博客主页和详细信息页面,请查看django基于类的视图:https://docs.djangoproject.com/en/3.2/ref/class-based-views/generic-display/

urls.py:

urlpatterns = [
path('', views.BlogListView.as_view(), name='blog_list'),
path('detail/<int:pk>', views.BlogDetailView.as_view(), name='blog_detail'),
]
# You can also use <slug:slug> instead of <int:pk>

views.py:

from blog.models import Blog
from django.views.generic import ListView, DetailView
class BlogListView(ListView):
model = Blog
class BlogDetailView(DetailView):
model = Blog

对于博客格式页面:https://github.com/django-ckeditor/django-ckeditor

https://medium.com/djangotube/django-ckeditor-install-with-youtube-and-code-spinet-plugins-b873c643f649

将您的消息/正文更改为RichTextUploadingField,然后您/用户可以根据自己的喜好使用文本格式化图像。

型号.py

from ckeditor_uploader.fields import RichTextUploadingField
class Blog(models.Model):
title = models.CharField(max_length=100)
message = RichTextUploadingField()

在您的设置中,按照GitHub指南设置CKEditor,您还必须将MEDIA_URL和MEDIA_ROOT添加到您的settings.py和projecturls.py文件中。

https://docs.djangoproject.com/en/3.2/howto/static-files/

项目/设置.py

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

project/uls.py

from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

相关内容