Django 20200407 博客开发 001

爱敲代码的三毛

我的博客开发(001)

文件结构:image.png

from django.db import models
from django.contrib.auth.models import User


# Create your models here.
class BlogType(models.Model):
    type_name = models.CharField(max_length=15)


    def __str__(self):
        return self.type_name


class Blog(models.Model):
    title = models.CharField(max_length=50)
    blog_type = models.ForeignKey(BlogType,on_delete=models.DO_NOTHING)
    content = models.TextField()
    author = models.ForeignKey(User,on_delete=models.DO_NOTHING)
    created_time = models.DateTimeField(auto_now_add = True)
    last_updated_time = models.DateTimeField(auto_now=True)


    def __str__(self):
        return "<Blog:%s>" % self.title

blog/views.py

from django.shortcuts import render,get_object_or_404
from .models import Blog,BlogType




# Create your views here.
def blog_list(request):
    context = {}
    context['blogs'] = Blog.objects.all()
    # 用于统计博客数量作为一个值放在字典context中,然后其键是blogs_count
    # context['blogs_count'] = Blog.objects.all().count()
    return render(request,"blog_list.html",context)


def blog_detail(request,blog_pk):
    context = {}
    context['blog'] = get_object_or_404(Blog,pk=blog_pk)
    return render(request,"blog_detail.html",context)


def blogs_with_type(request,blog_type_pk):
    context = {}
    blog_type = get_object_or_404(BlogType,pk=blog_type_pk)
    context["blogs"] = Blog.objects.filter(blog_type=blog_type)
    context['blog_type'] = blog_type
    return render(request,'blogs_with_type.html',context)

mysite/urls.py

from django.contrib import admin
from django.urls import path,include
from blog.views import blog_list
# from ..blog.views import blog_list


urlpatterns = [
    path('',blog_list,name="home"),  # 首页
    path('admin/', admin.site.urls),
    path('blog/',include('blog.urls')),
]

blog/urls.py

from . import views
from django.urls import path


urlpatterns = [
    # http://localhost/8000/blog/1
    path('<int:blog_pk>', views.blog_detail, name="blog_detail"),
    # http://localhost:8000/blog/  博客列表地址


    path('type/<int:blog_type_pk>',views.blogs_with_type,name="blogs_with_type"),


]

blog_detail.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{blog.title}}</title>
</head>
<body>
    <div>
        <a href="{% url 'home' %}">
            <h3>刘子君个人博客网站</h3>
        </a>
    </div>
    <h3>{{blog.title}}</h3>
    <p>作者:{{ blog.author }}</p>
    {# 这里为啥标红?另外这里年月日时分秒和Python的datetime中有点不一样,在分钟那html用n表示!H表示24进制小时,h表示12进制 #}
    <p>发表日期:{{ blog.created_time|date:"Y-m-d H:n:s" }}</p>
    <p>分类:<a href="{% url 'blogs_with_type' blog.blog_type.pk %}">{{ blog.blog_type }}
    </a></p>
    <p>{{blog.content}}</p>
</body>
</html>

blog_list.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>刘子君的博客</title>
</head>
<body>
        <div>
        <a href="{% url 'home' %}">
            <h3>刘子君个人博客网站</h3>
        </a>
    </div>
    <hr>


    {% for blog in blogs %}
{#        <a href="{ % url 'blog_detail' blog.pk % }">#}
            <a href="{% url 'blog_detail' blog.pk %}">
            <h3>{{blog.title}}</h3>
        </a>
{#            管道符表示过滤,这句话意思是对blogcontent中的内容过滤,只剩30字符显示,这里不知为啥起作用了但pycharm识别为错误#}
            <p>{{ blog.content|truncatechars:30 }}</p>
        {% empty %}
            <p>----- 敬请期待,暂无博文 -----</p>
    {% endfor %}
        {#        <p>一共有{{ blogs|length }}篇博文</p> {{ blogs_count }} {#        用于统计blogs中又多少条博文#}
        <p>一共有{{ blogs|length }}篇博文</p>
</body>
</html>

blogs_with_type.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>刘子君的博客</title>
</head>
<body>
        <div>
        <a href="{% url 'home' %}">
            <h3>刘子君个人博客网站</h3>
        </a>
    </div>
    <hr>


    {% for blog in blogs %}
{#        <a href="{ % url 'blog_detail' blog.pk % }">#}
            <a href="{% url 'blog_detail' blog.pk %}">
            <h3>{{blog.title}}</h3>
        </a>
{#            管道符表示过滤,这句话意思是对blogcontent中的内容过滤,只剩30字符显示,这里不知为啥起作用了但pycharm识别为错误#}
            <p>{{ blog.content|truncatechars:30 }}</p>
        {% empty %}
            <p>----- 敬请期待,暂无博文 -----</p>
    {% endfor %}
        {#        <p>一共有{{ blogs|length }}篇博文</p> {{ blogs_count }} {#        用于统计blogs中又多少条博文#}
        <p>一共有{{ blogs|length }}篇博文</p>
</body>
</html>

答疑:


# blog_list.html

<a href="{% url 'blog_detail' blog.pk %}">

image.png

{#            管道符表示过滤,这句话意思是对blogcontent中的内容过滤,只剩30字符显示,这里不知为啥起作用了但pycharm识别为错误#}
            <p>{{ blog.content|truncatechars:30 }}</p>
阅读 738

Python相关
Python相关,包含python开发接口、开发网站、数据爬取、自动化办公等

你,要怎样度过这一生?有的人二十岁已经死了,有的人七十岁还在发现生命的可能,有人终其一生,不知道...

19 声望
3 粉丝
0 条评论
你知道吗?

你,要怎样度过这一生?有的人二十岁已经死了,有的人七十岁还在发现生命的可能,有人终其一生,不知道...

19 声望
3 粉丝
宣传栏