From 7ab9ed8e17f61e729fa1b89db1c01fe71cfb75b326722fed01c148e2e67cb786 Mon Sep 17 00:00:00 2001 From: yuangyaa Date: Mon, 28 Jul 2025 00:53:46 +0800 Subject: [PATCH] Add rss && Change web_view --- myblog/blog/feeds.py | 96 +++++++ myblog/blog/templates/blog/contact.html | 325 ++++++++++++++++++++++++ myblog/blog/templates/blog/detail.html | 82 +++--- myblog/blog/templates/blog/index.html | 78 +++--- myblog/blog/templates/blog/rss.html | 302 ++++++++++++++++++++++ myblog/blog/urls.py | 11 +- myblog/blog/views.py | 24 ++ 7 files changed, 839 insertions(+), 79 deletions(-) create mode 100644 myblog/blog/feeds.py create mode 100644 myblog/blog/templates/blog/contact.html create mode 100644 myblog/blog/templates/blog/rss.html diff --git a/myblog/blog/feeds.py b/myblog/blog/feeds.py new file mode 100644 index 0000000..478b2a9 --- /dev/null +++ b/myblog/blog/feeds.py @@ -0,0 +1,96 @@ +from django.contrib.syndication.views import Feed +from django.urls import reverse +from django.shortcuts import get_object_or_404 +from .models import Post, Category + + +class LatestPostsFeed(Feed): + title = "六桂流芳的com" + link = "/rss/" + description = "最新博客文章" + # 添加content_type使浏览器能正确显示RSS内容 + content_type = 'application/xml; charset=utf-8' + + def items(self): + return Post.objects.order_by('-publish_date')[:10] + + def item_title(self, item): + return item.title + + def item_description(self, item): + return item.get_markdown_content() + + def item_link(self, item): + return reverse('detail', args=[item.pk]) + + +# 添加分类RSS Feed +class CategoryPostsFeed(Feed): + # 添加content_type使浏览器能正确显示RSS内容 + content_type = 'application/xml; charset=utf-8' + + def get_object(self, request, category_id): + return get_object_or_404(Category, pk=category_id) + + def title(self, obj): + return f"六桂流芳的com - {obj.name}分类" + + def link(self, obj): + return reverse('category_feed', args=[obj.pk]) + + def description(self, obj): + return f"{obj.name}分类的最新博客文章" + + def items(self, obj): + return Post.objects.filter(category=obj).order_by('-publish_date')[:10] + + def item_title(self, item): + return item.title + + def item_description(self, item): + return item.get_markdown_content() + + def item_link(self, item): + return reverse('detail', args=[item.pk]) + + +# 添加最新RSS Feed (与全部的区别在于数量限制) +class RecentPostsFeed(Feed): + title = "六桂流芳的com - 最新文章" + link = "/rss/recent/" + description = "最新的博客文章" + # 添加content_type使浏览器能正确显示RSS内容 + content_type = 'application/xml; charset=utf-8' + + def items(self): + return Post.objects.order_by('-publish_date')[:20] + + def item_title(self, item): + return item.title + + def item_description(self, item): + return item.get_markdown_content() + + def item_link(self, item): + return reverse('detail', args=[item.pk]) + + +# 添加全部RSS Feed +class AllPostsFeed(Feed): + title = "六桂流芳的com - 全部文章" + link = "/rss/all/" + description = "全部博客文章" + # 添加content_type使浏览器能正确显示RSS内容 + content_type = 'application/xml; charset=utf-8' + + def items(self): + return Post.objects.order_by('-publish_date') + + def item_title(self, item): + return item.title + + def item_description(self, item): + return item.get_markdown_content() + + def item_link(self, item): + return reverse('detail', args=[item.pk]) \ No newline at end of file diff --git a/myblog/blog/templates/blog/contact.html b/myblog/blog/templates/blog/contact.html new file mode 100644 index 0000000..9422353 --- /dev/null +++ b/myblog/blog/templates/blog/contact.html @@ -0,0 +1,325 @@ + + + + + 联系我 - 六桂流芳的com + + + + +
+ +
+ +
+ + +
+
+

联系我

+
+ 如果您有任何问题或想与我交流,可以通过以下方式联系我: +
+ +
    + {% if site_settings.contact_email %} +
  • + 邮箱: + {{ site_settings.contact_email }} +
  • + {% endif %} + {% if site_settings.contact_wechat %} +
  • + 微信: + {{ site_settings.contact_wechat }} +
  • + {% endif %} + {% if site_settings.contact_linkedin %} +
  • + LinkedIn: + {{ site_settings.contact_linkedin }} +
  • + {% endif %} + {% if site_settings.contact_github %} +
  • + GitHub: + {{ site_settings.contact_github }} +
  • + {% endif %} +
+
+
+
+ + + + \ No newline at end of file diff --git a/myblog/blog/templates/blog/detail.html b/myblog/blog/templates/blog/detail.html index 1593d46..3b4699b 100644 --- a/myblog/blog/templates/blog/detail.html +++ b/myblog/blog/templates/blog/detail.html @@ -15,14 +15,14 @@ max-width: 1200px; margin: 0 auto; padding: 20px; - / / 修改: 确保容器支持sticky定位 min-height: 100 vh; + min-height: 100vh; } .sidebar { width: 250px; padding-right: 20px; border-right: 1px solid #eee; - / / 修改: 添加侧边栏固定定位相关样式 position: -webkit-sticky; + position: -webkit-sticky; position: sticky; top: 20px; align-self: flex-start; @@ -160,35 +160,56 @@ padding: 5px 0; } - .contact-info { - background-color: #f9f9f9; - padding: 15px; - border-radius: 5px; - margin-top: 20px; - border: 1px solid #eee; + /* 添加导航栏样式 */ + .top-nav { + background-color: #007cba; + padding: 10px 0; + margin-bottom: 20px; } - .contact-info h3 { - margin-top: 0; - color: #333; - border-bottom: 2px solid #007cba; - padding-bottom: 10px; + .nav-container { + max-width: 1200px; + margin: 0 auto; + padding: 0 20px; } - .contact-info ul { - padding-left: 20px; + .nav-links { + list-style: none; + padding: 0; + margin: 0; + display: flex; } - .contact-info li { - margin: 10px 0; - color: #666; + .nav-links li { + margin-right: 20px; + } + + .nav-links li a { + color: white; + text-decoration: none; + padding: 8px 12px; + border-radius: 4px; + transition: background-color 0.3s; + } + + .nav-links li a:hover { + background-color: rgba(255, 255, 255, 0.2); } - -
-

六桂流芳的com

+ +
@@ -227,25 +248,6 @@ {% endfor %} - - -
-

联系我

-
    - {% if site_settings.contact_email %} -
  • 邮箱: {{ site_settings.contact_email }}
  • - {% endif %} - {% if site_settings.contact_wechat %} -
  • 微信: {{ site_settings.contact_wechat }}
  • - {% endif %} - {% if site_settings.contact_linkedin %} -
  • LinkedIn: {{ site_settings.contact_linkedin }}
  • - {% endif %} - {% if site_settings.contact_github %} -
  • GitHub: {{ site_settings.contact_github }}
  • - {% endif %} -
-
diff --git a/myblog/blog/templates/blog/index.html b/myblog/blog/templates/blog/index.html index 31d626f..5702253 100644 --- a/myblog/blog/templates/blog/index.html +++ b/myblog/blog/templates/blog/index.html @@ -160,35 +160,56 @@ footer { } - .contact-info { - background-color: #f9f9f9; - padding: 15px; - border-radius: 5px; - margin-top: 20px; - border: 1px solid #eee; + /* 添加导航栏样式 */ + .top-nav { + background-color: #007cba; + padding: 10px 0; + margin-bottom: 20px; } - .contact-info h3 { - margin-top: 0; - color: #333; - border-bottom: 2px solid #007cba; - padding-bottom: 10px; + .nav-container { + max-width: 1200px; + margin: 0 auto; + padding: 0 20px; } - .contact-info ul { - padding-left: 20px; + .nav-links { + list-style: none; + padding: 0; + margin: 0; + display: flex; } - .contact-info li { - margin: 10px 0; - color: #666; + .nav-links li { + margin-right: 20px; + } + + .nav-links li a { + color: white; + text-decoration: none; + padding: 8px 12px; + border-radius: 4px; + transition: background-color 0.3s; + } + + .nav-links li a:hover { + background-color: rgba(255, 255, 255, 0.2); } - -
-

六桂流芳的com

+ +
@@ -231,25 +252,6 @@ {% endfor %} - - -
-

联系我

-
    - {% if site_settings.contact_email %} -
  • 邮箱: {{ site_settings.contact_email }}
  • - {% endif %} - {% if site_settings.contact_wechat %} -
  • 微信: {{ site_settings.contact_wechat }}
  • - {% endif %} - {% if site_settings.contact_linkedin %} -
  • LinkedIn: {{ site_settings.contact_linkedin }}
  • - {% endif %} - {% if site_settings.contact_github %} -
  • GitHub: {{ site_settings.contact_github }}
  • - {% endif %} -
-
diff --git a/myblog/blog/templates/blog/rss.html b/myblog/blog/templates/blog/rss.html new file mode 100644 index 0000000..e7ec029 --- /dev/null +++ b/myblog/blog/templates/blog/rss.html @@ -0,0 +1,302 @@ + + + + + RSS订阅 - 六桂流芳的com + + + + + + +
+ + +
+
+

RSS订阅

+
+ RSS是一种用于发布经常更新的内容的网页格式。通过RSS阅读器,您可以订阅我们的内容,及时获取最新文章更新。 + 点击下面的链接可以在浏览器中查看RSS内容,使用RSS阅读器订阅时请复制链接地址。 +
+ + +
+
+
+ + + + \ No newline at end of file diff --git a/myblog/blog/urls.py b/myblog/blog/urls.py index 5b70b43..ce133ec 100644 --- a/myblog/blog/urls.py +++ b/myblog/blog/urls.py @@ -1,7 +1,16 @@ from django.urls import path from . import views +from .feeds import LatestPostsFeed, CategoryPostsFeed, RecentPostsFeed, AllPostsFeed urlpatterns = [ path('', views.index, name='index'), path('post//', views.detail, name='detail'), -] + # 添加RSS页面路由 + path('rss/page/', views.rss_page, name='rss_page'), + # 添加联系我页面路由 + path('contact/', views.contact_page, name='contact_page'), + path('rss/', LatestPostsFeed(), name='rss_feed'), + path('rss/category//', CategoryPostsFeed(), name='category_feed'), + path('rss/recent/', RecentPostsFeed(), name='recent_feed'), + path('rss/all/', AllPostsFeed(), name='all_feed'), +] \ No newline at end of file diff --git a/myblog/blog/views.py b/myblog/blog/views.py index 82e5300..859b769 100644 --- a/myblog/blog/views.py +++ b/myblog/blog/views.py @@ -67,3 +67,27 @@ def detail(request, post_id): except SiteSettings.DoesNotExist: site_settings = None return render(request, 'blog/detail.html', {'post': post, 'categories': categories, 'site_settings': site_settings}) + + +# 添加RSS页面视图 +def rss_page(request): + categories = Category.objects.all() + try: + site_settings = SiteSettings.objects.first() + except SiteSettings.DoesNotExist: + site_settings = None + return render(request, 'blog/rss.html', { + 'categories': categories, + 'site_settings': site_settings + }) + + +# 添加联系我页面视图 +def contact_page(request): + try: + site_settings = SiteSettings.objects.first() + except SiteSettings.DoesNotExist: + site_settings = None + return render(request, 'blog/contact.html', { + 'site_settings': site_settings + }) \ No newline at end of file