first commit

main
blog 6 months ago
parent f426199d3d
commit d8abcf9bc7

@ -39,7 +39,9 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'post'
'post',
'ckeditor',
'ckeditor_uploader'
]
MIDDLEWARE = [
@ -66,6 +68,7 @@ TEMPLATES = [
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'post.mycontextprocessors.getRightInfo',
],
},
},
@ -80,7 +83,7 @@ WSGI_APPLICATION = 'blog.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '180810db',
'NAME': 'blog',
'USER': 'root',
'PASSWORD': '123456',
'HOST': '127.0.0.1',
@ -111,15 +114,15 @@ AUTH_PASSWORD_VALIDATORS = [
# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
LANGUAGE_CODE = 'zh-Hans'
LANGUAGE_CODE = 'zh-Hans' #简体中文
TIME_ZONE = 'Asia/Shanghai'
TIME_ZONE = 'Asia/Shanghai' #时区
USE_I18N = True
USE_L10N = True
USE_TZ = True
USE_TZ =False
# Static files (CSS, JavaScript, Images)
@ -131,4 +134,10 @@ STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static','css')
]
# global_settings
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
CKEDITOR_UPLOAD_PATH = 'upload/'
# global_settings去复制

@ -13,10 +13,20 @@ Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from django.urls import path, include
from django.contrib import admin
from django.conf import settings
from blog.settings import DEBUG, MEDIA_ROOT
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('post.urls')),
path('admin/', admin.site.urls),
path('', include('post.urls')), # 使用path替换了url对于简单的根路径匹配不需要正则表达式
path('ckeditor/', include('ckeditor_uploader.urls')),
]
from django.urls import re_path
from django.views.static import serve
if settings.DEBUG:
urlpatterns += [
re_path(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
]

Binary file not shown.

After

Width:  |  Height:  |  Size: 683 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 69 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 769 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 754 KiB

@ -3,7 +3,7 @@
from django.apps import AppConfig
import os
default_app_config = 'post.PrimaryBlogConfig'
default_app_config = 'post.apps.PostConfig'
VERBOSE_APP_NAME = u"博客管理"

Binary file not shown.

@ -8,6 +8,7 @@ from .models import *
class PostModelAdmin(admin.ModelAdmin):
list_display = ('title','created')
admin.site.register(Category)
admin.site.register(Tag)
admin.site.register(Post,PostModelAdmin)

Binary file not shown.

@ -5,4 +5,5 @@ from django.apps import AppConfig
class PostConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'post'

@ -1,9 +1,7 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2018-08-10 07:51
from __future__ import unicode_literals
# Generated by Django 5.0.6 on 2024-05-28 10:01
from django.db import migrations, models
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
@ -18,39 +16,38 @@ class Migration(migrations.Migration):
name='Category',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('cname', models.CharField(max_length=30, unique=True)),
('cname', models.CharField(max_length=30, unique=True, verbose_name='类别名称')),
],
options={
'verbose_name_plural': '类别',
'db_table': 't_category',
},
),
migrations.CreateModel(
name='Post',
name='Tag',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=100, unique=True)),
('desc', models.CharField(max_length=100)),
('content', models.TextField()),
('created', models.DateTimeField(auto_now_add=True)),
('category', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='post.Category')),
('tname', models.CharField(max_length=30, unique=True, verbose_name='标签名称')),
],
options={
'db_table': 't_post',
'verbose_name_plural': '标签',
'db_table': 't_tag',
},
),
migrations.CreateModel(
name='Tag',
name='Post',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('tname', models.CharField(max_length=30, unique=True)),
('title', models.CharField(max_length=100, unique=True)),
('desc', models.CharField(max_length=100)),
('content', models.TextField()),
('created', models.DateTimeField(auto_now_add=True)),
('category', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='post.category')),
('tag', models.ManyToManyField(to='post.tag')),
],
options={
'db_table': 't_tag',
'verbose_name_plural': '帖子',
'db_table': 't_post',
},
),
migrations.AddField(
model_name='post',
name='tag',
field=models.ManyToManyField(to='post.Tag'),
),
]

@ -0,0 +1,34 @@
# Generated by Django 5.0.6 on 2024-05-28 16:55
import ckeditor_uploader.fields
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('post', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='category',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
migrations.AlterField(
model_name='post',
name='content',
field=ckeditor_uploader.fields.RichTextUploadingField(blank=True, null=True),
),
migrations.AlterField(
model_name='post',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
migrations.AlterField(
model_name='tag',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
]

@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from ckeditor_uploader.fields import RichTextUploadingField
# Create your models here.
class Category(models.Model): #类别
cname = models.CharField(max_length=30,unique=True,verbose_name='类别名称')
class Meta:
db_table = 't_category'
verbose_name_plural='类别'
def __str__(self):
# 首先检查self.cname是否为None或非字符串类型
if not isinstance(self.cname, str):
# 这里,您可以选择抛出一个具体的异常,或者返回一个默认值
# 例如,返回一个空字符串或者一个错误提示
raise ValueError("cname must be a string")
# 使用f-string进行字符串格式化
return f'Category: {self.cname}'
class Tag(models.Model): #标签
tname = models.CharField(max_length=30,unique=True,verbose_name='标签名称')
class Meta:
db_table = 't_tag'
verbose_name_plural = '标签'
def __str__(self):
# 首先检查self.cname是否为None或非字符串类型
if not isinstance(self.tname, str):
# 这里,您可以选择抛出一个具体的异常,或者返回一个默认值
# 例如,返回一个空字符串或者一个错误提示
raise ValueError("cname must be a string")
# 使用f-string进行字符串格式化
return f'Tag: {self.tname}'
class Post(models.Model):
title = models.CharField(max_length=100,unique=True) #标题
desc = models.CharField(max_length=100) #简述
content = RichTextUploadingField(null=True,blank=True)
created = models.DateTimeField(auto_now_add=True) #发帖时间
category = models.ForeignKey(Category,on_delete=models.CASCADE) #类别
tag = models.ManyToManyField(Tag) #标签
class Meta:
db_table = 't_post'
verbose_name_plural = '帖子'
def __str__(self):
# 首先检查self.cname是否为None或非字符串类型
if not isinstance(self.title, str):
# 这里,您可以选择抛出一个具体的异常,或者返回一个默认值
# 例如,返回一个空字符串或者一个错误提示
raise ValueError("cname must be a string")
# 使用f-string进行字符串格式化
return f'Post:{self.title}'

Binary file not shown.

@ -0,0 +1,23 @@
#coding=utf-8
from django.db.models import Count
from post.models import Post
def getRightInfo(request):
#1.获取分类信息
r_catepost = Post.objects.values('category__cname','category').annotate(c=Count('*')).order_by('-c')
#2.近期文章
r_recpost = Post.objects.all().order_by('-created')[:3]
#3.获取日期归档信息 原生查询
from django.db import connection
cursor = connection.cursor()
cursor.execute("select created,count('*') c from t_post GROUP BY DATE_FORMAT(created,'%Y-%m') ORDER BY c desc,created desc")
# 获取查询结果
r_filepost = cursor.fetchall()
# 返回获取的标签信息以字典的形式
return {'r_catepost':r_catepost,'r_recpost':r_recpost,'r_filepost':r_filepost}

@ -0,0 +1,35 @@
{% extends 'base.html' %}
{% block title %}
帖子列表
{% endblock %}
{% block left %}
<div id="main">
<div class="archives">
{% for post in postList %}
<article class="archive-article archive-type-post">
<div class="archive-article-inner">
<header class="archive-article-header">
<a href="#" class="archive-article-date">
<time>{{ post.created|date:'Y-m' }}</time>
</a>
<h1 itemprop="name">
<a class="archive-article-title" target="_blank" href="/post/{{ post.id }}">{{ post.title }}</a>
</h1>
</header>
</div>
</article>
{% endfor %}
</div>
</div>
{% endblock %}

@ -1,5 +1,5 @@
{% extends 'base.html' %}
{% load myfilter %}
{% load myfilter %} <!--加载文件夹-->
{% block title %}
详情页面
@ -27,10 +27,10 @@
<h2>前言</h2>
{{ post.desc }}
<hr>
{{ post.content|md|safe }}
{{ post.content|md|safe }} <!--safe是自动解析-->
</div>
</div>
</article>
</article> <!--分享操作,借用百度的接口-->
<div class="bdsharebuttonbox"><a href="#" class="bds_more" data-cmd="more"></a><a href="#" class="bds_qzone" data-cmd="qzone"></a><a href="#" class="bds_tsina" data-cmd="tsina"></a><a href="#" class="bds_tqq" data-cmd="tqq"></a><a href="#" class="bds_renren" data-cmd="renren"></a><a href="#" class="bds_weixin" data-cmd="weixin"></a></div>
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdPic":"","bdStyle":"0","bdSize":"16"},"share":{},"image":{"viewList":["qzone","tsina","tqq","renren","weixin"],"viewText":"分享到:","viewSize":"16"},"selectShare":{"bdContainerClass":null,"bdSelectMiniList":["qzone","tsina","tqq","renren","weixin"]}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
</div>

@ -2,16 +2,40 @@
{% block title %}首页{% endblock %}
{% block headerjs %}
<style>
form {
position: relative;
width: 150px;
margin: 0 auto;
}
.d1{
float: right;
line-height: 67px;
}
.d1 input {
width: 100px;
height: 30px;
border: 2px solid darkred;
border-radius: 5px;
outline: none;
background: white;
color: #1e242a;
}
</style>
{% endblock %}
{% block left %}
<div id="main">
{% for post in postList %}
<article class="article article-type-post">
<div class="article-meta">
<a class="article-date">
<time>{{ post.created|date:'Y-m-d H:i:s' }}</time>
<time>{{ post.created|date:'Y-m-d H:i:s' }}</time>
</a>
<div class="article-category">
<a class="article-category-link" href="#" target="_blank">{{ post.category.cname }}</a>
@ -26,11 +50,11 @@
<div class="article-entry" itemprop="articleBody">
<h2>前言</h2>
<hr>
{{ post.desc }}
{{ post.desc }}
<p class="article-more-link">
<a href="/post/{{ post.id }}" target="_blank">阅读全文</a>
<a href="/post/{{ post.id }}" target="_blank">阅读全文</a>
</p>
</div>
<footer class="article-footer">
@ -45,46 +69,29 @@
{% endfor %}
</ul>
</footer>
</div>
</article>
{% endfor %}
<nav id="page-nav">
{% if postList.has_previous %}
{% if postList.has_previous %} <!--判断是否有上一页-->
<a class="extend prev" rel="next" href="/page/{{ postList.previous_page_number }}">« Prev</a>
{% endif %}
{% for page in pageList %}
{% if currentNum == page %}
{% if currentNum == page %} <!--判断是否是当前页-->
<span class="page-number current">{{ page }}</span>
{% else %}
<a class="page-number" href="/page/{{ page }}">{{ page }}</a>
{% endif %}
{% endfor %}
{% if postList.has_next %}
<a class="extend next" rel="next" href="/page/{{ postList.next_page_number }}">Next »</a>
{% if postList.has_next %} <!--判断是否有下一页-->
<a class="extend next" rel="next" href="/page/{{ postList.next_page_number }}">Next »</a> <!--传递下一页的参数-->
{% endif %}
@ -93,19 +100,4 @@
</div>
{% endblock %}
{% endblock %}

@ -0,0 +1,12 @@
#coding=utf-8
from django.urls import path, re_path
from . import views # 确保从当前目录导入views模块前面的import语句有误
urlpatterns = [
path('', views.queryAll, name='home'),
re_path(r'^page/(?P<num>\d+)$', views.queryAll, name='pagination'),
re_path(r'^post/(?P<postid>\d+)$', views.detail, name='post_detail'),
re_path(r'^category/(?P<cid>\d+)$', views.queryPostByCid, name='category_posts'),
re_path(r'^archive/(?P<year>\d+)/(?P<month>\d+)$', views.queryPostByCreated, name='archive'),
]

Binary file not shown.

@ -2,17 +2,13 @@
from __future__ import unicode_literals
from django.shortcuts import render
# Create your views here.
#渲染主页面
# from post.models import Post
from templates.models import Post
from post.models import Post
from django.core.paginator import Paginator
import math
def queryAll(request,num=1):
num = int(num)
#获取所有帖子信息
@ -53,4 +49,17 @@ def detail(request,postid):
#根据postid查询帖子的详情信息
post = Post.objects.get(id=postid)
return render(request,'detail.html',{'post':post})
return render(request,'detail.html',{'post':post})
#根据类别id查询所有帖子
def queryPostByCid(request,cid):
postList = Post.objects.filter(category_id=cid)
# Post.objects.filter(category__id=cid)
return render(request,'article.html',{'postList':postList})
#根据发帖时间查询所有帖子
def queryPostByCreated(request,year,month):
postList = Post.objects.filter(created__year=year,created__month=month)
return render(request,'article.html',{'postList':postList})

Binary file not shown.

@ -5,8 +5,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" type="text/css" href="/static/style.css">
<link rel="stylesheet" type="text/css" href="/static/style.css"> <!--引入静态css文件-->
<style>
form {
@ -39,7 +38,7 @@
{% include 'header.html' %}
<div class="outer">
{% block left %}
{% block left %} <!--左边栏-->
{% endblock %}

@ -0,0 +1,68 @@
<aside id="sidebar">
<!--分类-->
<div class="widget-wrap">
<h3 class="widget-title">分类</h3>
<div class="widget">
<ul class="category-list">
{% for cp in r_catepost %}
<li class="category-list-item">
<a class="category-list-link" href="/category/{{ cp.category }}">{{ cp.category__cname }}</a>
<span class="category-list-count">{{ cp.c }}</span>
</li>
{% endfor %}
</ul>
</div>
</div>
<!--归档-->
<div class="widget-wrap">
<h3 class="widget-title">归档</h3>
<div class="widget">
<ul class="archive-list">
{% for fp in r_filepost %}
<li class="archive-list-item">
<a class="archive-list-link" href="/archive/{{ fp.0|date:'Y' }}/{{ fp.0|date:'m' }}">{{ fp.0|date:'Y年m月' }}</a>
<span class="archive-list-count">{{ fp.1 }}</span>
</li>
{% endfor %}
</ul>
</div>
</div>
<!--近期文章-->
<div class="widget-wrap">
<h3 class="widget-title">近期文章</h3>
<div class="widget">
<ul>
{% for rp in r_recpost %}
<li>
<a href="/post/{{ rp.id }}" target="_blank">{{ rp.title|truncatechars:10 }}</a>
</li>
{% endfor %}
</ul>
</div>
</div>
<div class="widget-wrap">
<h3 class="widget-title">友情链接</h3>
<div class="widget">
<ul>
<li>
<a href="http://www.bjsxt.com/" target="_blank">尚学堂</a>
</li>
<li>
<a href="http://www.sxt.cn/" target="_blank">速学堂</a>
</li>
</ul>
</div>
</div>
</aside>

Binary file not shown.

Binary file not shown.

@ -1,42 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Category(models.Model):
cname = models.CharField(max_length=30,unique=True,verbose_name=u'类别名称')
class Meta:
db_table = 't_category'
verbose_name_plural=u'类别'
def __unicode__(self):
return u'Category:%s'%self.cname
class Tag(models.Model):
tname = models.CharField(max_length=30,unique=True,verbose_name=u'标签名称')
class Meta:
db_table = 't_tag'
verbose_name_plural = u'标签'
def __unicode__(self):
return u'Tag:%s' % self.tname
class Post(models.Model):
title = models.CharField(max_length=100,unique=True)
desc = models.CharField(max_length=100)
content = models.TextField()
created = models.DateTimeField(auto_now_add=True)
category = models.ForeignKey(Category,on_delete=models.CASCADE)
tag = models.ManyToManyField(Tag)
class Meta:
db_table = 't_post'
verbose_name_plural = u'帖子'
def __unicode__(self):
return u'Post:%s' % self.title

Binary file not shown.

@ -1,10 +0,0 @@
#coding=utf-8
from django.conf.urls import url
import views
urlpatterns=[
url(r'^$',views.queryAll),
url(r'^page/(\d+)$',views.queryAll),
url(r'^post/(\d+)$',views.detail),
]

Binary file not shown.

Binary file not shown.

@ -1,109 +0,0 @@
<aside id="sidebar">
<!--分类-->
<div class="widget-wrap">
<h3 class="widget-title">分类</h3>
<div class="widget">
<ul class="category-list">
<li class="category-list-item">
<a class="category-list-link" href="http://hello123.pythonanywhere.com/category/1">前端</a>
<span class="category-list-count">4</span>
</li>
<li class="category-list-item">
<a class="category-list-link" href="http://hello123.pythonanywhere.com/category/2">后端</a>
<span class="category-list-count">3</span>
</li>
<li class="category-list-item">
<a class="category-list-link" href="http://hello123.pythonanywhere.com/category/3">数据库</a>
<span class="category-list-count">1</span>
</li>
</ul>
</div>
</div>
<!--归档-->
<div class="widget-wrap">
<h3 class="widget-title">归档</h3>
<div class="widget">
<ul class="archive-list">
<li class="archive-list-item">
<a class="archive-list-link" href="http://hello123.pythonanywhere.com/archive/2018/06">2018年06月</a>
<span class="archive-list-count">6</span>
</li>
<li class="archive-list-item">
<a class="archive-list-link" href="http://hello123.pythonanywhere.com/archive/2017/05">2017年05月</a>
<span class="archive-list-count">1</span>
</li>
<li class="archive-list-item">
<a class="archive-list-link" href="http://hello123.pythonanywhere.com/archive/2017/06">2017年06月</a>
<span class="archive-list-count">1</span>
</li>
</ul>
</div>
</div>
<!--近期文章-->
<div class="widget-wrap">
<h3 class="widget-title">近期文章</h3>
<div class="widget">
<ul>
<li>
<a href="http://hello123.pythonanywhere.com/post/8" target="_blank">T4</a>
</li>
<li>
<a href="http://hello123.pythonanywhere.com/post/7" target="_blank">T3</a>
</li>
<li>
<a href="http://hello123.pythonanywhere.com/post/4" target="_blank">MySQL表连接</a>
</li>
</ul>
</div>
</div>
<div class="widget-wrap">
<h3 class="widget-title">友情链接</h3>
<div class="widget">
<ul>
<li>
<a href="http://www.bjsxt.com/" target="_blank">尚学堂</a>
</li>
<li>
<a href="http://www.sxt.cn/" target="_blank">速学堂</a>
</li>
</ul>
</div>
</div>
</aside>
Loading…
Cancel
Save