Add link_me in the view
This commit is contained in:
@@ -17,14 +17,20 @@ class PostAdmin(admin.ModelAdmin):
|
||||
|
||||
|
||||
class SiteSettingsAdmin(admin.ModelAdmin):
|
||||
list_display = ('summary_length',)
|
||||
|
||||
# 保持所有字段在列表中显示,方便直接编辑
|
||||
list_display = ('id', 'summary_length', 'contact_email', 'contact_wechat', 'contact_linkedin', 'contact_github')
|
||||
list_display_links = ('id',)
|
||||
|
||||
def has_add_permission(self, request):
|
||||
# 限制只能有一个站点设置实例
|
||||
return not SiteSettings.objects.exists()
|
||||
|
||||
def has_delete_permission(self, request, obj=None):
|
||||
# 禁止删除站点设置
|
||||
return False
|
||||
|
||||
|
||||
# 注册自定义的PostAdmin
|
||||
admin.site.register(Post, PostAdmin)
|
||||
admin.site.register(Category)
|
||||
admin.site.register(SiteSettings, SiteSettingsAdmin)
|
||||
admin.site.register(SiteSettings, SiteSettingsAdmin)
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
# Generated by Django 5.1 on 2025-07-27 15:53
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('blog', '0008_sitesettings'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='sitesettings',
|
||||
name='contact_email',
|
||||
field=models.EmailField(blank=True, help_text='联系邮箱', max_length=254, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='sitesettings',
|
||||
name='contact_github',
|
||||
field=models.URLField(blank=True, help_text='GitHub链接', null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='sitesettings',
|
||||
name='contact_linkedin',
|
||||
field=models.URLField(blank=True, help_text='LinkedIn链接', null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='sitesettings',
|
||||
name='contact_wechat',
|
||||
field=models.CharField(blank=True, help_text='微信号', max_length=100, null=True),
|
||||
),
|
||||
]
|
||||
@@ -22,7 +22,11 @@ class Category(models.Model):
|
||||
# 添加站点设置模型
|
||||
class SiteSettings(models.Model):
|
||||
summary_length = models.IntegerField(default=50, help_text="文章摘要字符长度")
|
||||
|
||||
contact_email = models.EmailField(blank=True, null=True, help_text="联系邮箱")
|
||||
contact_wechat = models.CharField(max_length=100, blank=True, null=True, help_text="微信号")
|
||||
contact_linkedin = models.URLField(blank=True, null=True, help_text="LinkedIn链接")
|
||||
contact_github = models.URLField(blank=True, null=True, help_text="GitHub链接")
|
||||
|
||||
class Meta:
|
||||
verbose_name = "站点设置"
|
||||
verbose_name_plural = "站点设置"
|
||||
|
||||
@@ -159,6 +159,30 @@
|
||||
background-color: white;
|
||||
padding: 5px 0;
|
||||
}
|
||||
|
||||
.contact-info {
|
||||
background-color: #f9f9f9;
|
||||
padding: 15px;
|
||||
border-radius: 5px;
|
||||
margin-top: 20px;
|
||||
border: 1px solid #eee;
|
||||
}
|
||||
|
||||
.contact-info h3 {
|
||||
margin-top: 0;
|
||||
color: #333;
|
||||
border-bottom: 2px solid #007cba;
|
||||
padding-bottom: 10px;
|
||||
}
|
||||
|
||||
.contact-info ul {
|
||||
padding-left: 20px;
|
||||
}
|
||||
|
||||
.contact-info li {
|
||||
margin: 10px 0;
|
||||
color: #666;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
@@ -203,6 +227,25 @@
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
|
||||
<!-- 添加联系我模块 -->
|
||||
<div class="contact-info">
|
||||
<h3>联系我</h3>
|
||||
<ul>
|
||||
{% if site_settings.contact_email %}
|
||||
<li>邮箱: {{ site_settings.contact_email }}</li>
|
||||
{% endif %}
|
||||
{% if site_settings.contact_wechat %}
|
||||
<li>微信: {{ site_settings.contact_wechat }}</li>
|
||||
{% endif %}
|
||||
{% if site_settings.contact_linkedin %}
|
||||
<li>LinkedIn: {{ site_settings.contact_linkedin }}</li>
|
||||
{% endif %}
|
||||
{% if site_settings.contact_github %}
|
||||
<li>GitHub: {{ site_settings.contact_github }}</li>
|
||||
{% endif %}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="main-content">
|
||||
|
||||
@@ -159,6 +159,30 @@
|
||||
|
||||
footer {
|
||||
}
|
||||
|
||||
.contact-info {
|
||||
background-color: #f9f9f9;
|
||||
padding: 15px;
|
||||
border-radius: 5px;
|
||||
margin-top: 20px;
|
||||
border: 1px solid #eee;
|
||||
}
|
||||
|
||||
.contact-info h3 {
|
||||
margin-top: 0;
|
||||
color: #333;
|
||||
border-bottom: 2px solid #007cba;
|
||||
padding-bottom: 10px;
|
||||
}
|
||||
|
||||
.contact-info ul {
|
||||
padding-left: 20px;
|
||||
}
|
||||
|
||||
.contact-info li {
|
||||
margin: 10px 0;
|
||||
color: #666;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
@@ -207,6 +231,25 @@
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
|
||||
<!-- 添加联系我模块 -->
|
||||
<div class="contact-info">
|
||||
<h3>联系我</h3>
|
||||
<ul>
|
||||
{% if site_settings.contact_email %}
|
||||
<li>邮箱: {{ site_settings.contact_email }}</li>
|
||||
{% endif %}
|
||||
{% if site_settings.contact_wechat %}
|
||||
<li>微信: {{ site_settings.contact_wechat }}</li>
|
||||
{% endif %}
|
||||
{% if site_settings.contact_linkedin %}
|
||||
<li>LinkedIn: {{ site_settings.contact_linkedin }}</li>
|
||||
{% endif %}
|
||||
{% if site_settings.contact_github %}
|
||||
<li>GitHub: {{ site_settings.contact_github }}</li>
|
||||
{% endif %}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="main-content">
|
||||
|
||||
@@ -22,6 +22,7 @@ def index(request):
|
||||
site_settings = SiteSettings.objects.first()
|
||||
summary_length = site_settings.summary_length if site_settings else 50
|
||||
except SiteSettings.DoesNotExist:
|
||||
site_settings = None
|
||||
summary_length = 50
|
||||
|
||||
# 根据分类和搜索关键词筛选文章
|
||||
@@ -53,11 +54,16 @@ def index(request):
|
||||
'categories': categories,
|
||||
'selected_category': category_id,
|
||||
'query': query,
|
||||
'search_type': search_type
|
||||
'search_type': search_type,
|
||||
'site_settings': site_settings
|
||||
})
|
||||
|
||||
|
||||
def detail(request, post_id):
|
||||
post = get_object_or_404(Post, pk=post_id)
|
||||
categories = Category.objects.all() # 获取所有分类用于侧边栏
|
||||
return render(request, 'blog/detail.html', {'post': post, 'categories': categories})
|
||||
try:
|
||||
site_settings = SiteSettings.objects.first()
|
||||
except SiteSettings.DoesNotExist:
|
||||
site_settings = None
|
||||
return render(request, 'blog/detail.html', {'post': post, 'categories': categories, 'site_settings': site_settings})
|
||||
|
||||
Reference in New Issue
Block a user