django-30行核心代码,开发简易博客网站

1.models.py
博客一般由标签,标题,内容,发布时间等组成。以下是博客的核心字段

class BlogType(models.Model):  
    name = models.CharField(max_length=100)  
    add_time = models.DateTimeField(auto_now_add=True)  
    update_time = models.DateTimeField(auto_now=True)

    def __str__(self):  
        return self.name  


class Blog(models.Model):  
    category = models.ManyToManyField(BlogType)  
    title = models.CharField(max_length=200)  
    content = models.TextField()  
    add_time = models.DateTimeField(auto_now_add=True)  
    update_time = models.DateTimeField(auto_now=True)  

    def __str__(self):  
        return self.title  

2.views.py

from django.views.generic import ListView, DetailView  
from .models import BlogType, Blog

class BlogTypeListView(ListView):  
    model = BlogType  
    template_name = 'blog_type.html'  
    paginate_by = 2  # 每页多少数据  


class BlogListView(ListView):  
    model = Blog  
    template_name = 'blog_list.html'  
    paginate_by = 2  


class BlogDetailView(DetailView):  
    model = Blog  
    template_name = 'blog_detail.html'  
    pk_url_kwarg = 'pk'  


class BlogTypeToBlog(ListView):  
    template_name = 'blog_list.html'  
    model = BlogType  
    context_object_name = 'blog_list'  

    def get_queryset(self):  
        super().get_queryset()  
        queryset = BlogType.objects.get(pk=self.kwargs.get('pk')).blog_set.all()  # 通过类型的id反向查询博客表中的关联文章  
        return queryset  

3.urls.py

path('blogtype/', BlogTypeListView.as_view(), name='blog_type'),  
path('blog_list/', BlogListView.as_view(), name='blog_list'),  
path('detail/<int:pk>', BlogDetailView.as_view(), name='blog_detail'),  
path('tag/<int:pk>', BlogTypeToBlog.as_view(), name='blog_tag')  

4.templates
4.1 blog_detail.html

<body>  
{{ object.title }}  
{% for foo in object.category.all %}  
    {{ foo.name }}  
{% endfor %}

{{ object.content }}  
</body>  

4.2 blog_list.html
关于分页的文档链接:https://docs.djangoproject.com/zh-hans/3.0/topics/pagination/#paginating-a-listview

<body>  
<ul>  
    {% for blog in blog_list %}  
        <li>标题:<a href="{% url 'book:blog_detail' blog.pk %}">{{ blog.title }}</a>,内容:{{ blog.content }},标签:  
            {% for i in blog.category.all %}  
                <a href="{% url 'book:blog_tag' i.pk %}">{{ i.name }}</a>  
            {% endfor %}  
        </li>  
    {% endfor %}  
    {% if page_obj.has_previous %}  
        <a href="?page=1">&laquo; first</a>  
        <a href="?page={{ page_obj.previous_page_number }}">previous</a>  
    {% endif %}

    <span class="current">  
        Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.  
    </span>  

    {% if page_obj.has_next %}  
        <a href="?page={{ page_obj.next_page_number }}">next</a>  
        <a href="?page={{ page_obj.paginator.num_pages }}">last &raquo;</a>  
    {% endif %}  
</ul>  
</body>  

4.3 blog_type.html

<body>  
<ul>  
    {% for blogtype in blogtype_list %}  
        <li>{{ blogtype.name }}</li>  
    {% endfor %}  
</ul>  
</body>  

示例图片:
blog_type

blog_list

blog_tag

发布了5 篇原创文章 · 获赞 1 · 访问量 3785
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览