Django简单博客系统项目开发项目源码

  • A+
所属分类:Django技术 网站首页

1.MODEL设计

 
  1. #myblog\blog\models.py  
  2. from django.db import models  
  3.   
  4.   
  5. from datetime import datetime  
  6. from django.db import models  
  7.  
  8.   
  9. class ArticleBlog(models.Model):  
  10.     title = models.CharField(max_length=50,default='小小',verbose_name=u'标题')  
  11.     content = models.TextField(help_text='博客内容',verbose_name=u'内容')  
  12.     pubtime = models.DateTimeField(default=datetime.now, verbose_name=u"添加时间")  
  13.   
  14.   
  15.     class Meta:  
  16.         verbose_name = "博客"  
  17.         verbose_name_plural = verbose_name  
  18.         ordering = ["-pubtime"]  
  19.   
  20.   
  21.     def __str__(self):  
  22.         return self.title  

2.VIEWS设计

  1. #myblog\blog\views.py  
  2.   
  3. #coding=utf-8  
  4. from django.shortcuts import render  
  5. from django.shortcuts import HttpResponse  
  6.   
  7. from . import models  
  8.   
  9.   
  10. def index(request):  
  11.     articles = models.ArticleBlog.objects.all()  
  12.     return render(request, 'index.html', { 'articles': articles})  
  13.   
  14.   
  15. def article_page(request, article_id):  
  16.     article = models.ArticleBlog.objects.get(pk=article_id)  
  17.     return  render(request,'article_page.html',{'article':article})  
  18.   
  19.   
  20. def edit_page(request,article_id):  
  21.     if str(article_id) == '0':  
  22.         return render(request,'edit_page.html')  
  23.     article = models.ArticleBlog.objects.get(pk=article_id)  
  24.     return render(request,'edit_page.html',{'article':article})  
  25.   
  26.   
  27. def edit_action(request):  
  28.     title = request.POST.get('title','标题')  
  29.     content = request.POST.get('content','博客内容')  
  30.     article_id = request.POST.get('article_id','0')  
  31.   
  32.     if article_id == '0':  
  33.         models.ArticleBlog.objects.create(title=title,content=content)  
  34.         articles = models.ArticleBlog.objects.all()  
  35.         return render(request,'index.html',{'articles':articles})  
  36.   
  37.     article = models.ArticleBlog.objects.get(pk=article_id)  
  38.     article.title = title  
  39.     article.content = content  
  40.     article.save()  
  41.     return render(request, 'article_page.html', {'article': article})  

3.URL设计

1)根URLS设计

  1. #myblog\urls.py  
  2. from django.conf.urls import url,include  
  3. from django.contrib import admin  
  4. from blog.views import *  
  5.   
  6.   
  7.   
  8. urlpatterns = [  
  9.     url(r'^admin/', admin.site.urls),  
  10.     url(r'^blog/', include('blog.urls',namespace='blog')),  
  11.   
  12. ]  

2)应用URLS设计

  1. #myblog\blog\urls.py  
  2. # -*- coding:utf-8 -*-  
  3. __author__ = "moonrong"  
  4. __date__ = "2018/7/15 15:15"  
  5.   
  6. from django.conf.urls import url  
  7. from . import views  
  8.   
  9.   
  10. urlpatterns = [  
  11.     url(r'^$', views.index),  
  12.     url(r'^article/(?P<article_id>[0-9]+)$', views.article_page, name='article_page'),  
  13.     url(r'^edit/(?P<article_id>[0-9]+)$', views.edit_page,name='edit_page'),  
  14.     url(r'^edit/action$',views.edit_action,name='edit_action'),  
  15. ]  

4.模板设计

1)博客主页面index.html代码编写

  1. <html>  
  2. <body>  
  3. <h1>基于Django开发的博客系统</h1>  
  4. <h5>  
  5.     <a href="{% url 'blog:edit_page' 0 %}">【新建文章】</a>  
  6. </h5>  
  7. <h4>博文列表</h4>  
  8. {% for article in articles %}  
  9.         <a href="{%  url 'blog:article_page' article.id  %} ">{{ article.title }}</a>  
  10.           <p>{{  article.pubtime }}</p>  
  11.     <h4></h4>  
  12.     </br>  
  13.   
  14. {%  endfor %}  
  15. </body>  
  16. </html>  

2)博客内容页面article_page.html

  1. <body>  
  2.     <h1>下面是博客内容页</h1>  
  3.     <h4></h4>  
  4. <br>  
  5. <h3>{{ article.title }}</h3>  
  6. </br>  
  7. <h3>{{ article.content }}</h3>  
  8.     <h4></h4>  
  9. </br></br>  
  10.   
  11.     <a href="{% url 'blog:edit_page' article.id %}">修改文章</a>  
  12. </body>  

3)博客撰写页面edit_page.html

  1. <body>  
  2. <form action="{% url 'blog:edit_action' %}" method="post">  
  3.     {% csrf_token %}  
  4.      {% if article %}  
  5.          <input type="hidden" name="article_id" value="{{ article.id }}"/>  
  6.     <label>文章标题  
  7.         <input type="text" name="title" value="{{ article.title }}"/>  
  8.     </label>  
  9.     <br/>  
  10.      <label>文章内容  
  11.         <input type="text" name="content" value="{{ article.content }}"/>  
  12.     </label>  
  13.     <br/>  
  14.     {% else %}  
  15.      <input type="hidden" name="article_id" value="0"/>  
  16.     <label>文章标题  
  17.         <input type="text" name="title" />  
  18.     </label>  
  19.     <br/>  
  20.      <label>文章内容  
  21.         <input type="text" name="content" />  
  22.     </label>  
  23.     <br/>  
  24.     {% endif %}  
  25.     <input type="submit" value="提交">  
  26.   
  27. </form>  
  28.   
  29. </body>  

5.相关配置项目

1)应用apps.py文件配置

  1. from django.apps import AppConfig  
  2.   
  3.   
  4. class BlogConfig(AppConfig):  
  5.     name = 'blog'  

2)注册后台管理admin.py配置

  1. # -*- coding:utf-8 -*-  
  2. from django.contrib import admin  
  3. from .models import ArticleBlog  
  4.   
  5.   
  6. # Register your models here.  
  7. # 定义ArticleBlog的管理器  
  8. class ArticleBlogAdmin(admin.ModelAdmin):  
  9.     list_display = [ 'title', 'content','pubtime']  
  10.   
  11.     search_fields = ['title', 'content']  
  12.     list_filter = ['title', 'pubtime']  
  13.   
  14.   
  15. # 将管理器与model进行注册关联  
  16. admin.site.register(ArticleBlog, ArticleBlogAdmin)  

moonrong
  • 版权声明:本站原创文章,于2019年3月19日13:44:01,由 发表,共 4530 字。
  • 版权声明: 本文由于2019年3月19日13:44:01 发表在 好派笔记,共 4530 字。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: