Python之Django框架开发博客

来源:互联网 发布:手机天使玻璃优化软件 编辑:程序博客网 时间:2024/05/16 17:52

先来一张目录结构图


1、第一步,必然是向创建目录啦!   ,打开命令行,进入想要安置项目的目录

命令行输入: django-admin  startproject myblog



1、博客系统初始界面 ,显示文章列表和添加新文章按钮

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>博客</title></head><body><h1><a href ="{% url "blog:edit_page" %}">添加新文章</a></h1><center>{% for article in articles %}    <a href ="{% url "blog:article_page" article.id %}">{{ article.id }}、{{ article.title }}</a>    <br/><br/>{% endfor %}</center></body></html>

2、点击某一文章,进入文章详情,显示文章详情,和修改文章按钮

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>文章页面</title></head><body><center>    <h1>{{ article.title }}</h1>    <h3>{{ article.content }}</h3></center><br><br><a href ="{% url "blog:reset_action" article.id %}">修改文章</a></body></html>
3、点击添加新文章或修改文章都进入 编辑文章界面(修改文章时进入编辑界面需要显示原有数据)
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>撰写文章</title></head><body><center><form action="{% url "blog:edit_action" %}" method = "POST">    {% csrf_token %}    <input type="hidden" name ="id" value = {{ article.id }}>    <label>文章标题        <input type="text" name ="title" value = {{ article.title }}>    </label>    <br><br>    <label>文章内容        <input type="text" name ="content" value = {{ article.content }}>    </label>    <br><br>    <label>        <input type="submit" value ="提交">    </label></form></center></body></html>

接下来开始写后台代码,和一些必要的配置

Django默认使用SQLite,如果你想使用Mysql的话  可以直接去我的上一篇文章中进行两步配置即可 ,传送门:http://blog.csdn.net/chou_out_man/article/details/75514998

hiahia~~

连接数据库前先创建model

# -*-coding : UTF-8 -*-from django.db import models#一个model对应数据库的一张表# Create your models here.#ORM 对象关系映射#影藏了数据访问细节,不需要写SQL语句class Article(models.Model):    title = models.CharField(max_length=32 )    content = models.TextField(null=True)    #为了在Django中admin自带的后台管理界面直接显示文章标题    def __str__(self):        return self.title

人狠话不多,直接上后台代码!

在你新建的model的views.py中编写

# -*-coding = UTF-8 -*-from django.shortcuts import renderfrom . import modelsfrom django.http import HttpResponse#每个响应对应一个函数#每个函数必须返回一个响应#函数必须有一个参数,一般约定为request#每个响应函数对应一个url#获取所有文章,返回到指定页面def index (request):    articles = models.Article.objects.all()    return render(request, "blog/index.html", {"articles": articles})#根据文章编号,获取文章详细内容,返回到指定界面def article_page(request,article_id):    article = models.Article.objects.get(id = article_id)    return render(request,"blog/article_page.html",{"article" : article})#接受添加文章请求,跳转到指定界面def edit_page(request):    return render(request,"blog/article_editpage.html")#编辑文章界面发出的请求 ,获取文章内容,如果是添加新文章(id为空时,默认为0)则向数据库插入数据#如果是修改文章(id!=0),则修改数据库id对应的文章def edit_action(request):    id = request.POST.get("id")    title = request.POST.get("title")    content = request.POST.get("content")    if(str(id)=="0"):        models.Article.objects.create(title=title,content=content)        articles = models.Article.objects.all()        return  render(request,"blog/index.html",{"articles":articles})    else :        models.Article.objects.filter(id=id).update(title= title,content = content)        article = models.Article.objects.get(id = id)        return render(request,"blog/article_page.html",{"article" : article})#接受修改文章请求,根据id修改指定的文章def reset_action(request,article_id):    article = models.Article.objects.get(id=article_id)    return render(request,"blog/article_editpage.html",{"article":article})

配置url

当然是在项目容器的urls.py中配置啦

但是在实际开发中可能会有能多很多页面需要配置,所以这里 使用软链接链接到应用中在进行实际的配置

在容器中urls.py       注意使用include时需要先进行导入操作

"""myblog URL ConfigurationThe `urlpatterns` list routes URLs to views. For more information please see:    https://docs.djangoproject.com/en/1.9/topics/http/urls/Examples:Function views    1. Add an import:  from my_app import views    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')Class-based views    1. Add an import:  from other_app.views import Home    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')Including another URLconf    1. Import the include() function: from django.conf.urls import url, include    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))"""from django.conf.urls import urlfrom django.contrib import adminfrom django.conf.urls import url,includefrom blog import viewsurlpatterns = [    url(r'^admin/', admin.site.urls),    url(r'^blog/', include("blog.urls",namespace="blog"))]
然后再进入应用l的urls.py中进行实际配置
from django.conf.urls import urlfrom . import viewsurlpatterns =[    url(r'^index/$',views.index),#文章列表页面    url(r'^article/(?P<article_id>[0-9]+)$',views.article_page,name = "article_page"),    url(r'^edit/$', views.edit_page , name = "edit_page"),    url(r'^edit/action$', views.edit_action , name = "edit_action"),    url(r'reset/action/(?P<article_id>[0-9]+)$', views.reset_action,name = "reset_action")]


接下来,一个超级简单(没有注册、登录,没有用户权限管理、没有收费的)的博客就大功告成啦!

python manage.py runserver 查看自己的博客ba !