我想创建一个博客网站。我已经创建了网站的主页,在我的博客网站上有4篇文章。我想通过点击打开一篇文章,它会将我重定向到唯一的文章页面。每个文章页面都有很少的图片、标题和分行符。如何使用django将这些内容上传到我的博客模型?示例文章页面...See the article page's picture
发布于 2021-04-25 19:06:04
有几种方法可以做到这一点,但我建议将CKEditor用于Django
有关博客主页和详细信息页面,请查看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
将您的邮件/正文更改为RichTextUploadingField,然后您/用户可以使用您认为合适的文本格式化图像。
models.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和项目urls.py文件中。
https://docs.djangoproject.com/en/3.2/howto/static-files/
project/settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'project/urls.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)https://stackoverflow.com/questions/67252302
复制相似问题