一、Django-admin
1、创建表和注册表
settings.py文件:
TIME_ZONE = 'Asia/Shanghai' #支持中文数据
models.py文件内容:
from django.db import models from django.contrib.auth.models import AbstractUser class UserInfo(AbstractUser): """ 用户信息 """ nid = models.AutoField(primary_key=True) telephone = models.CharField(max_length=11, null=True, unique=True) avatar = models.FileField(upload_to='avatars/', default="/avatars/default.png") create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True) blog = models.OneToOneField(to='Blog', to_field='nid', null=True) def __str__(self): return self.username class Blog(models.Model): """ 博客(个人站点)信息 """ nid = models.AutoField(primary_key=True) title = models.CharField(verbose_name='个人博客标题', max_length=64) site = models.CharField(verbose_name='个人博客后缀', max_length=32, unique=True) theme = models.CharField(verbose_name='博客主题', max_length=32) def __str__(self): return self.title class Category(models.Model): """ 博主个人文章分类表 """ nid = models.AutoField(primary_key=True) title = models.CharField(verbose_name='分类标题', max_length=32) blog = models.ForeignKey(verbose_name='所属博客', to='Blog', to_field='nid') def __str__(self): return self.title class Tag(models.Model): nid = models.AutoField(primary_key=True) title = models.CharField(verbose_name='标签名称', max_length=32) blog = models.ForeignKey(verbose_name='所属博客', to='Blog', to_field='nid') def __str__(self): return self.title class Article(models.Model): nid = models.AutoField(primary_key=True) title = models.CharField(max_length=50, verbose_name='文章标题') desc = models.CharField(max_length=255, verbose_name='文章描述') create_time = models.DateTimeField(verbose_name='创建时间') comment_count=models.IntegerField(default=0) up_count=models.IntegerField(default=0) down_count=models.IntegerField(default=0) category = models.ForeignKey(to='Category', to_field='nid', null=True) user = models.ForeignKey(verbose_name='作者', to='UserInfo', to_field='nid') tags = models.ManyToManyField( to="Tag", through='Article2Tag', through_fields=('article', 'tag'), ) def __str__(self): return self.title class ArticleDetail(models.Model): """ 文章详细表 """ nid = models.AutoField(primary_key=True) content = models.TextField() article = models.OneToOneField(to='Article', to_field='nid') class Article2Tag(models.Model): nid = models.AutoField(primary_key=True) article = models.ForeignKey(verbose_name='文章', to="Article", to_field='nid') tag = models.ForeignKey(verbose_name='标签', to="Tag", to_field='nid') class Meta: unique_together = [ ('article', 'tag'), ] def __str__(self): v = self.article.title + "--" + self.tag.title return v class ArticleUpDown(models.Model): """ 点赞表 """ nid = models.AutoField(primary_key=True) user = models.ForeignKey('UserInfo', null=True) article = models.ForeignKey("Article", null=True) is_up = models.BooleanField(default=True) class Meta: unique_together = [ ('article', 'user'), ] class Comment(models.Model): """ 评论表 """ nid = models.AutoField(primary_key=True) article = models.ForeignKey(verbose_name='评论文章', to='Article', to_field='nid') user = models.ForeignKey(verbose_name='评论者', to='UserInfo', to_field='nid') content = models.CharField(verbose_name='评论内容', max_length=255) create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True) parent_comment = models.ForeignKey('self', null=True) def __str__(self): return self.content
admin.py文件内容:(注册表)
from django.contrib import admin from .models import * admin.site.register(UserInfo) admin.site.register(Blog) admin.site.register(Category) admin.site.register(Tag) admin.site.register(Article) admin.site.register(Article2Tag) admin.site.register(ArticleDetail) admin.site.register(ArticleUpDown) admin.site.register(Comment)
urls.py文件内容:
from django.conf.urls import url,include from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), ]
2、登录录入数据
登录的账号必须是超级用户,我们用自己创建的用户root
3、admin路由规则
/admin/app01/role/ 查看角色列表 /admin/app01/role/add/ 添加角色 /admin/app01/role/2/change/ 编辑 /admin/app01/role/2/delete/ 删除 /admin/app01/userinfo/ /admin/应用名/表名 /admin/app01/userinfo/add/ /admin/应用名/表名/功能名 /admin/app01/userinfo/1/change/ /admin/app01/userinfo/1/delete/
二、博客项目主页
1、FileField与ImageField分析
(1)项目的根目录下存储文件 userinfo表设置: class userinfo: avatar = models.FileField(upload_to='avatars/', default="/avatars/default.png") 视图函数获取文件对象: avatar = request.FILES.get("avatar_img") UserInfo.objects.create_user(username=user, password=pwd, email=email, avatar=avatar) Django会在项目的根目录创建avatars文件夹,将上传文件下载到该文件夹中,avatar字段保存的是文件的相对路径。 (2)指定目录下存储文件 如果settings.py配置 MEDIA_ROOT=os.path.join(BASE_DIR, "blog", "media") Django会MEDIA_ROOT下创建avatars文件夹,将上传文件下载到该文件夹中,avatar字段保存的是文件的相对路径。 (3)用户直接访问media目录 url.py文件设置: from django.conf.urls import url,include from django.contrib import admin from django.views.static import serve from cnblog_s20 import settings urlpatterns = [ url(r'^media/(?P.*)$', serve, {'document_root': settings.MEDIA_ROOT}), ] settings.py文件: MEDIA_URL = "/media/" #添加的别名 最后浏览器可以直接访问: http://127.0.0.1:8000/media/avatars/lu.jpg,即可以看到上传的图片
2、导航条
如果用户是登录状态,就让显示登录状态;如果用户还没有登录,就在导航条上显示登录,注册,在html页面做if判断
-
{% if request.user.username %} {#用户是登录状态#}
- {{ request.user.username }} {#显示当前用户的用户名#}
- 注销
- Dropdown {% else %}
- 登录
- 注册 {% endif %}
三、博客首页页面及视图函数
1、视图函数
views.py文件添加index函数:
def index(request): article_list=Article.objects.all() #得到所有文章的列表 return render(request,"index.html",locals())
2、博客首页页面
index.html页面:
Title Panel title
Panel contentPanel title
Panel contentPanel title
Panel content{% for article in article_list %}{{ article.title }}
{#显示文章标题#}{#显示用户头像#}{{ article.desc }} {#显示文章摘要内容#}{{ article.user.username }} 发布于 {{ article.create_time|date:'Y-m-d H:i' }} {# 表示一个空格 #} 评论({{ article.comment_count }}) 点赞({{ article.up_count }}) {# vertical-align: -3px表示向下偏移3px #}
{% endfor %}Panel title
Panel content
首页显示效果如下图: