1-首先创建项目 项目名称随意取(我这里叫做BK)
2-创建app app取名为boke
1-在BK.settings.py文件中配置
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'boke',
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'BK',
'USER': 'root',
'PASSWORD': 'qwe123',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
STATICFILES_DIRS=[
os.path.join(BASE_DIR, 'static')
]
2-在BK目录下__init__.py文件中配置
这里需要下载pymysql库
下载命令: pip install pymysql
import pymysql
pymysql.install_as_MySQLdb()
3-在boke目录下创建urls.py文件后 在BK.urls.py中配置
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('boke/', include('boke.urls')),
]
4-在boke.modles中创建表模型
from django.db import models
# Create your models here.
class Blog(models.Model):
title = models.CharField(max_length=20)
content = models.TextField()
5-在boke.views.py文件中写好视图函数
from django.shortcuts import render,redirect,reverse
from django.views import View
from django.http import HttpResponse
from boke.models import Blog
# Create your views here.
def index(request):
return render(request,'boke/index.html')
#添加 博客
def add(request):
#判断请求方式
if request.method == 'GET':
return render(request,'boke/add.html')
elif request.method == 'POST':
#添加数据 获取标题以及内容
title = request.POST.get('title')
content = request.POST.get('content')
#print(title,content,"=============")
#将数据存入数据库
blog,i = Blog.objects.get_or_create(title=title,content=content)
#print(blog,"--------------")
return HttpResponse("添加文章成功") if i else HttpResponse("输入数据重复 请重新输入")
else:
return HttpResponse("请求方式有误")
#博客详情页
def detail(request,blog_id):
id = Blog.objects.filter(id=blog_id).first()
return render(request,'boke/detail.html',context={
'id':id,
})
#博客列表页
def list(request):
#获取数据中的对象
list_all = Blog.objects.all()
return render(request,'boke/list.html', context={
'list_all':list_all,
})
#删除博客
def delete(request,blog_id):
Blog.objects.get(id=blog_id).delete()
return redirect(reverse("boke:list"))
#编辑博客
def edit(request,blog_id):
id = Blog.objects.filter(id=blog_id).first()
if id:
if request.method == "POST":
title = request.POST.get("title")
content = request.POST.get("content")
Blog.objects.filter(id=blog_id).update(title=title,content=content)
return redirect(reverse("boke:list"))
else:
return render(request, 'boke/edit.html', context={
"id": id,
})
return HttpResponse("操作方式异常")
6-在boke.urls.py文件中配置路由
from django.urls import path
from . import views
app_name ='boke'
urlpatterns =[
path('index/',views.index,name="index"),
path('add/',views.add,name="add"),
path('list/',views.list,name="list"),
path('edit/<int:blog_id>/',views.edit,name="edit"),
path('detail/<int:blog_id>/',views.detail,name="detail"),
path('delete/<int:blog_id>/',views.delete,name="delete"),
]
7-在BK.templates模板文件夹下创建boke文件夹 然后在boke文件夹下分别创建base.html,add.html,index.html,list.html,detail.html,edit.html,
base.html 为模板 基类 其余html分别就继承base.html 以及在其基础上进行添加和修改
(1)templates/boke/base.html文件下代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
(2)index.html为首页
{% extends 'boke/base.html' %}
{% block title %}博客首页{% endblock %}
{% block content %}
<table>
<tr>
<td><a href="{% url 'boke:add' %}">添加文章</a></td>
<td><a href="{% url 'boke:list' %}">文章列表</a></td>
</tr>
</table>
{% endblock %}
(3)add.html 添加文章
{% extends 'boke/base.html' %}
{% block title %}添加博客{% endblock %}
{% block content %}
<h1>添加新博客</h1>
<form action="" method="post">
{% csrf_token %}
标 题: <input type="text" placeholder="请输入标题" name="title">
文章内容: <input type="text" placeholder="请输入文章内容" name="content">
<input type="submit" value="发布博客">
</form>
{% endblock %}
(4)list.html为文章列表
{% extends 'boke/base.html' %}
{% block title %}文章列表{% endblock %}
{% block content %}
<table>
<tr>
<td width="200px" height="50px">文章</td>
<td width="200px" height="50px">基本操作</td>
</tr>
{% for list in list_all %}
<tr>
<td><a href="{% url 'boke:detail' list.id %}">{{ list.content }}</a></td>
<td>
<a href="{% url 'boke:edit' list.id %}">编辑博客</a>
<a href="{% url 'boke:delete' list.id %}">删除博客</a>
</td>
</tr>
{% endfor %}
</table>
{% endblock %}
(5)detail.html 文章的详情页
{% extends 'boke/base.html' %}
{% block title %}博客详情页{% endblock %}
{% block content %}
<h1 style="color: saddlebrown">博客标题:{{ id.title }}</h1>
<h2 style="color: blue; font-size: 40px">文章内容:{{ id.content }}</h2>
{% endblock %}
(6)edit.html 为文章编辑
{% extends 'boke/base.html' %}
{% block content %}
<h1 style="color: red;">编辑文章</h1>
<form action="" method="post">
{% csrf_token %}
标题: <input width="200px" height="100px"type="text" placeholder="请输入标题" name="title" value="{{ id.title }}"><br>
文章内容: <textarea width="200px" height="100px"name="content" placeholder="请修改文章内容">{{ id.content }}</textarea><br>
<input type="submit" value="编辑博客" >
</form>
{% endblock %}
(7)最后是删除