利用django写一个博客系统

萌新第一次利用django写一个博客系统

搭建环境 利用pycharm
1.新建一个python项目
使用pip install django=3.0.0
在这里插入图片描述
然后使用命令django-admin startproject [项目名]创建项目
在这里插入图片描述
使用python manage.py startapp [app名]创建app
在这里插入图片描述
查看目录结构在这里插入图片描述
添加应用
在这里插入图片描述
在APP中models.py写入自己的model
在这里插入图片描述

model的代码

from django.db import models

# Create your models here.
class ListInfo(models.Model):
    articleName=models.CharField(max_length=20)
    create_date=models.DateField()
    def __str__(self):
        return self.articleName
class ArticleInfo(models.Model):
    articleTitle=models.CharField(max_length=20)
    article=models.CharField(max_length=10000)
    gender=models.BooleanField(default=True)
    list=models.ForeignKey(ListInfo,on_delete=models.CASCADE)

生成迁移文件
python manage.py makemigrations

在这里插入图片描述
根据迁移文件生成数据库创建表
python manage.py migrate在这里插入图片描述
生成数据库文件
在这里插入图片描述

使用SQLite对数据库文件进行编辑和查看
在这里插入图片描述
配置项目的url和app的url
在这里插入图片描述
在这里插入图片描述
创建目录index.html,新建一个和app平级的tempare文件夹,在tempare文件夹下创建index.html,更改项目的settings文件配置
在这里插入图片描述
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<ul>
    {%for list in articleList %}
    <li>
       <a href="{% url 'blogApp:article_page' list.id%}">
    {{list.articleName}}
       </a>
    </li>
    {%endfor%}
    <a href="{% url 'blogApp:create_article' %}">增加新文章</a>
</ul>
</body>
</html>

效果如下图在这里插入图片描述
然后创建博客的html article.html
在这里插入图片描述
article代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
{% load static %}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jisuanji</title>
<style type="text/css">
.box1{
    background:url("{% static "img2/shutterstock_16717870.jpg"%}") no-repeat;
	height:100%;
	}
.box1 span{
	font-size:50px;
	color:#000;
	display:inline-block;
	margin:20px auto;
	text-align:center;
	width:100%;}
.box2{
    padding: 0px;
    height: 500px;
    width: 100%;
	margin:0px;
	background:#6C3;
	font-size:20px;
	display:inline-block;
	}
.p{
	text-indent:2em;
	color:#000;}
.box3{
    font-size: 18px;
    margin-top: 0px;
    padding: 0px;
    text-align: center;
	background:url("{% static "img2/bg-0782.gif"%}");
	height:125px;}
 .article{
     text-indent: 2em;
        color: brown;
    }
.words{
    color: crimson;
}

</style>
</head>

<body>
<div class="box1">
{%for article in articleList%}
<span>{{article.articleTitle}}</span>
</div>
<div class="box2">
    <p class="article">
        {{article.article}}
    </p>
</div>
<div class="box3">
    <br>
    <a href="{% url 'blogApp:change_article' article.id%}" class="words">修改当前文章</a>
    <p> <a href="index" class="words"> 返回博客主页</a></p>
    {%endfor%}
</div>
</body>
</html>

在django中如果要使用图片的话要先建一个和tempares平级的文件夹,然后将图片放入,同时要更改settings中的配置在这里插入图片描述
在这里插入图片描述
注释掉下图中的文件在这里插入图片描述

而且还要在html文件的开始加入如下的{$ load static $} 在这里插入图片描述

然后是创建博客的html和views中的方法,记得每进行一个html的配置就要更改一次urls文件中的url
在这里插入图片描述
html代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

{% load static %}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jisuanji</title>
<style type="text/css">
.title{
    font-size: 20px;
    background:url("{% static "img2/shutterstock_16717870.jpg"%}") no-repeat;
	height:110px;
    text-align: center;
}
.article{
      padding: 0px;
    height: 500px;
    width: 100%;
	margin:0px;
	background:#6C3;
	font-size:20px;
	display:inline-block;
    text-align: center;
}
.background3{
    font-size: 18px;
    margin-top: 0px;
    padding: 0px;
    text-align: center;
	background:url("{% static "img2/bg-0782.gif"%}");
	height:125px;
}
#title{
    vert-align: top;
    width: 50%;
    height: 50px;
}
#article{
    width: 90%;
    height: 90%;
}
#false{
    width: 10%;
    height: 30%;
}
#sure{
    width: 10%;
    height: 30%;
}
</style>
</head>

<body>
<form action="{%url 'blogApp:edit_action'%}" method="post">
<div class="title">
    <span>
    <a>文章标题</a><br>
    <input type="text" id="title" name="title" value=""/>
</span>
</div>
<div class="article">
    <a>文章内容</a><br>
    <textarea id="article"  name="article"></textarea>
</div>
<div class="background3">
    <br>
    <br>
    <input type="submit" id="sure" name="sure" value="确定"/>
    <input type="button" id="false" name="false" value="取消"onclick="window.open('index.html')"/>
</div>
</form>
</body>
</html>

在这里插入图片描述
在这里插入图片描述
然后是对博客的修改的html changArticle.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

{% load static %}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jisuanji</title>
<style type="text/css">
.title{
    font-size: 20px;
    background:url("{% static "img2/shutterstock_16717870.jpg"%}") no-repeat;
	height:110px;
    text-align: center;
}
.article{
      padding: 0px;
    height: 500px;
    width: 100%;
	margin:0px;
	background:#6C3;
	font-size:20px;
	display:inline-block;
    text-align: center;
}
.background3{
    font-size: 18px;
    margin-top: 0px;
    padding: 0px;
    text-align: center;
	background:url("{% static "img2/bg-0782.gif"%}");
	height:125px;
}
#title{
    vert-align: top;
    width: 50%;
    height: 50px;
}
#article{
    width: 90%;
    height: 90%;
}
#false{
    width: 10%;
    height: 30%;
}
#sure{
    width: 10%;
    height: 30%;
}
</style>
</head>

<body>
<form action="{%url 'blogApp:change_action'%}" method="post">
    <input type="hidden" id="articleId" name="articleId" value="{{article.id}}"/>
<div class="title">
    <span>
    <a>文章标题</a><br>
    <input type="text" id="title" name="title" value="{{article.articleTitle}}"/>
</span>
</div>
<div class="article">
    <a>文章内容</a><br>
    <textarea id="article"  name="article">{{article.article}}</textarea>
</div>
<div class="background3">
    <br>
    <br>
    <input type="submit" id="sure" name="{{article.id}}" value="确定"/>
    <input type="button" id="false" name="false" value="取消"onclick="window.open('index.html')"/>
</div>
</form>
</body>
</html>

views中的代码如下

from django.shortcuts import render
from django.http import HttpResponse
from .models import  *
from django.db.models import Max
from datetime import datetime
# Create your views here.

def index(request):
    list=ListInfo.objects.all()
    context={"articleList":list}
    return render(request,"blog/index2.html",context)

def details(request,id):
    list=ListInfo.objects.get(id=id).articleinfo_set.all()
    context={"articleList":list}
    return render(request,'blog/article/article.html',context)

def change(request,id):
    list=ArticleInfo.objects.get(id=id)
    context={"article":list}
    return render(request,"blog/article/changeArticle.html",context)

def edit_action(request):
    #print(ArticleInfo.objects.all().aggregate(Max("id")))
    #print(ArticleInfo.objects.all().aggregate(Max("id")).get("id__max"))
    articleMax=ArticleInfo.objects.all().aggregate(Max("id")).get("id__max")
    listMax= ListInfo.objects.all().aggregate(Max("id")).get("id__max")
    title=request.POST.get("title","TITLE")
    content=request.POST.get("article","ARTICLE")
    add=ArticleInfo(articleTitle=title,article=content,id=articleMax+1,list_id=listMax+1)
    add1 = ListInfo(articleName=title,id=listMax+1,create_date="2020-03-08")
    add1.save()
    add.save()
    articles=ListInfo.objects.all()
    context={"articleList":articles}
    return render(request,"blog/index2.html",context)

def create_article(request):
    return render(request,"blog/article/createArticle.html")

def change_action(request):
    retid=request.POST.get("sure","SURE")
    title = request.POST.get("title", "TITLE")
    content = request.POST.get("article", "ARTICLE")
    article=ArticleInfo.objects.get(id=retid)
    article.articleTitle=title
    article.article=content
    list=ListInfo.objects.get(id=article.list_id)
    list.articleName=title
    list.save()
    article.save()
    articles=ListInfo.objects.all()
    context={"articleList":articles}
    return render(request, "blog/index2.html", context)

urls中的配置

from . import views
from django.conf.urls import url

app_name="[blogApp]"
urlpatterns=[
    url("index",views.index,name="index"),
    url("article/(\d+)",views.details,name="article_page"),
    url("change/(\d+)",views.change,name="change_article"),
    url("create",views.create_article,name="create_article"),
    url("edit/action$",views.edit_action,name="edit_action"),
    url("change/action",views.change_action,name="change_action")
]

在html中可以通过app名:url名来调用对应的url
比如要调用第二个url,可以在html方法中使用

<a href="{% url 'blogApp:create_article' %}">

但是必须要在app的url文件添加app的名字
app_name="[app名]"

button按钮可以通过οnclick=“url”来点击跳转到另外的界面
传输数据要通过format标签和submit传入数据,然后在对应的方法中进行增删改查

<body>
<form action="{%url 'blogApp:edit_action'%}" method="post">
<div class="title">
    <span>
    <a>文章标题</a><br>
    <input type="text" id="title" name="title" value=""/>
</span>
</div>
<div class="article">
    <a>文章内容</a><br>
    <textarea id="article"  name="article"></textarea>
</div>
<div class="background3">
    <br>
    <br>
    <input type="submit" id="sure" name="sure" value="确定"/>
    <input type="button" id="false" name="false" value="取消"onclick="window.open('index.html')"/>
</div>
</form>
</body>

在更改博客时可以通过html的隐藏域来讲博客的id传入到后端中,对数据库进行更改
代码如下

<input type="hidden" id="articleId" name="articleId" value="{{article.id}}"/>

不过在写标签的input的时候,千万不要忘记input结尾有一个/,不然在后端中使用request.POST.get(“articleId”,“ARTICLEID”)就无法获得id值,而是你定义的默认的ARTICLEID

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值