Django開發簡單的blog
阿新 • • 發佈:2018-06-04
port static 首頁 == .get orm 模板 詳情 ble
本項目基於學習階段,只是涉及簡單的crud與模板變量等操作.
1.創建blog app項目:
python manage.py startapp blog,並在項目主目錄的settings.py中註冊該app
INSTALLED_APPS = [ ‘django.contrib.admin‘, ‘django.contrib.auth‘, ‘django.contrib.contenttypes‘, ‘django.contrib.sessions‘, ‘django.contrib.messages‘, ‘django.contrib.staticfiles‘, ‘book‘, ‘common‘, ‘info‘, ‘blog‘, ]
2.創建好app後,首先創建數據庫模型models.py:
from django.db import models # Create your models here. class Blog(models.Model): id = models.AutoField(primary_key=True) title = models.CharField(max_length=30) content = models.TextField(max_length=300)
並通過manage.py管理工具生成遷移文件,並映謝到數據庫中.
3.在blog APP中創建templates模板文件目錄並在裏面創建blog目錄
主要頁面有:主頁,列表頁,添加頁,編輯頁,詳情頁與模板基類頁
base.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> {% block title %} 標題 {% endblock %}</title> </head><body> {% block body %} 內容 {% endblock %} </body> </html>
add.html:
{% extends ‘blog/base.html‘ %} {% block title %} 添加文章 {% endblock %} {% block body %} <h1>添加文章</h1><br> <form action="" method="post"> {% csrf_token %} 標題:<input type="text" name="title"><br> 內容:<textarea placeholder="請輸入內容" name="content" cols="30" rows="10"></textarea><br> <button type="submit">發布文章</button> </form> {% endblock %}
index.html:
{% extends ‘blog/base.html‘ %} {% block title %} 首頁 {% endblock %} {% block body %} <tr> <td><a href={% url ‘blog_add‘ %}>添加文章</a></td> <td><a href={% url ‘blog_list‘ %}>文章列表</a></td> </tr> {% endblock %}
list.html:
{% extends ‘blog/base.html‘ %} {% block title %} 文章列表 {% endblock %} {% block body %} <h1>文章列表</h1> <table width="400px"> <thead> <tr> <th>標題</th> <th>操作</th> </tr> </thead> <tbody> {% for blog in blogs %} <tr> <th><a href={% url ‘blog_detail‘ blog.id %}>{{ blog.title }}</a></th> <th><a href={% url ‘blog_modify‘ blog.id %}>編輯</a>|<a href={% url ‘blog_delete‘ blog.id %}>刪除</a></th> </tr> {% endfor %} </tbody> </table> {% endblock %}
edit.html:
{% extends ‘blog/base.html‘ %} {% block title %} 修改文章 {% endblock %} {% block body %} <h1>修改文章</h1><br> <form action="" method="post"> {% csrf_token %} 標題:<input type="text" name="title" value={{ blog.title }}><br> 內容:<textarea placeholder="請輸入內容" name="content" cols="30" rows="10">{{ blog.content }}</textarea><br> <button type="submit">修改</button> </form> {% endblock %}
detail.html:
{% extends ‘blog/base.html‘ %} {% block title %} {{ blog.title }} {% endblock %} {% block body %} <h1>文章詳情</h1> {{ blog.content }} {% endblock %}
4.創建好模板文件後,創建相對應的urls.py:
from django.conf.urls import url from . import views urlpatterns = [ url(r‘^$‘, views.index,name=‘blog_index‘), url(r‘^add/$‘, views.add,name=‘blog_add‘), url(r‘^list/$‘, views.list,name=‘blog_list‘), url(r‘^detail/(?P<blog_id>\d+)$‘, views.detail,name=‘blog_detail‘), url(r‘^delete/(?P<blog_id>\d+)$‘, views.delete,name=‘blog_delete‘), url(r‘^modify/(?P<blog_id>\d+)$‘, views.modify,name=‘blog_modify‘), ]
5.創建相對應的視圖文件views.py:
from django.shortcuts import render,HttpResponse,reverse,redirect
from .models import Blog
# Create your views here.
def index(request):
return render(request,‘blog/index.html‘)
def add(request):
if request.method == ‘GET‘:
return render(request,‘blog/add.html‘)
elif request.method == ‘POST‘:
#獲取表單內容
title = request.POST.get(‘title‘)
content = request.POST.get(‘content‘)
#添加到數據庫中
Blog.objects.create(title=title,content=content)
return HttpResponse(‘發布成功‘)
else:
return HttpResponse(‘這是不能處理的操作‘)
def list(request):
blogs = Blog.objects.all()
return render(request,‘blog/list.html‘,{‘blogs‘:blogs})
def detail(request,blog_id=0):
blog = Blog.objects.get(id=blog_id)
return render(request,‘blog/detail.html‘,{‘blog‘:blog})
def delete(request,blog_id=0):
blog = Blog.objects.get(id=blog_id)
if blog:
blog.delete()
return redirect(reverse(‘blog_list‘))
else:
return HttpResponse(‘要刪除的數據不存在‘)
def modify(request,blog_id=0):
blog = Blog.objects.get(id=blog_id)
if blog:
if request.method == ‘GET‘:
return render(request, ‘blog/edit.html‘, context={‘blog‘: blog})
elif request.method == ‘POST‘:
title = request.POST.get(‘title‘)
content = request.POST.get(‘content‘)
blog.title = title
blog.content = content
blog.save()
return HttpResponse(‘修改成功‘)
else:
return HttpResponse(‘該操作無法執行‘)
else:
return HttpResponse(‘數據不存在‘)
Django開發簡單的blog