1.项目结构:
2.Blog.settings.py中的INSTALLED_APPS添加'index',属性值。可以搜索到index页面。
3.配置主从urls:在Blog里配置主urls,可以映射到从urls。这样配置可以方便以后的URL管理。
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^blog/', include("index.urls")),
]
4.开发index主项目:
index/admin.py
class ArticleAdmin(admin.ModelAdmin):
list_display = ['id','title','context','author','Article_classification','create_time']
list_filter = ('create_time','Article_classification')
#为后台添加的分类器
admin.site.register(Article,ArticleAdmin)
5.index/models.py
class Article(models.Model):
title=models.CharField(max_length=32,default='title')
context=models.TextField(null=True,blank=True)
create_time=models.DateField(auto_now=True)
author=models.CharField(max_length=32,default='author')
Article_classification=models.CharField(max_length=64,default='默认分类')
def __str__(self):
return self.title
6.index/urls.py
app_name="blog"
urlpatterns = [
url(r'^index/', views.index,name="index"),
url(r'^detail/(?P<art_id>[0-9]+)', views.article_content,name='detail'),
url(r'^edit/(?P<art_id>[0-9]+)', views.article_edit,name="edit"),
url(r'^edit_action/',views.edit_action,name="edit_action"),
url(r'^dele_article/(?P<art_id>[0-9]+)',views.dele_article,name="dele_article"),
]
7.index/views.py
from django.shortcuts import render
from .models import Article
# Create your views here.
def index(request):
articles=Article.objects.all()
return render(request,"index.html",{'articles':articles})
def article_content(request,art_id):#文章详情页
artcle=Article.objects.get(pk=art_id)
return render(request,"content.html",{'art':artcle})
def article_edit(request,art_id):#文章编辑页
if str(art_id)=='0':
return render(request,"edit.html")
else:
art=Article.objects.get(pk=art_id)
return render(request,"edit.html",{'art':art})
def edit_action(request):#文章修改和发布新文章
title=request.POST.get("title","DEFAULT")
content=request.POST.get("content","DEFAULT")
author=request.POST.get("author","author")
classification=request.POST.get("classification","默认分类")
art_id=request.POST.get("art_id","0")
if art_id=='0':
Article.objects.create(title=title,context=content,author=author,Article_classification=classification)
arts=Article.objects.all()
return render(request,"index.html",{'articles':arts})
else:
article=Article.objects.get(pk=art_id)
article.title=title
article.context=content
article.author=author
article.Article_classification=classification
article.save()
article=Article.objects.get(pk=art_id)
return render(request,"content.html",{'art':article})
def dele_article(request,art_id):
Article.objects.get(pk=art_id).delete()
article = Article.objects.all()
return render(request,"index.html",{'articles':article})
3.html模板开发:
3.1详情页content
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文章详情页</title>
</head>
<body>
<label>主题:</label><h2>{{ art.title }}</h2>
<label>详情:</label><h3>{{ art.context }}</h3>
<label>文章作者:</label><h3>{{ art.author }}</h3>
<label>文章分类:</label><h3>{{ art.Article_classification }}</h3>
<br>
<br>
<a style="text-decoration-line: none" href="{% url 'blog:edit' art.id %}">修改文章</a>
<br>
<a style="text-decoration-line: none" href="{% url 'blog:dele_article' art.id %}">删除文章</a>
<br>
<a style="text-decoration-line: none" href="{% url 'blog:index' %}">返回主页</a>
</body>
</html>
3.2编辑页edit
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>发布新文章</title>
</head>
<body>
<form action="{% url "blog:edit_action" %}" method="post">
{% csrf_token %}
{% if art %}
<input type="hidden" value="{{ art.id }}" name="art_id">
<br>
<label><h3>文章标题</h3></label>
<input type="text" name="title" value="{{ art.title }}">
<br>
<br>
<label><h3>文章内容</h3></label>
<input type="text" name="content" value="{{ art.context }}">
<br>
<label><h3>文章作者</h3></label>
<input type="text" name="author" value="{{ art.author }}">
<br>
<label><h3>文章分类</h3></label>
<input type="text" name="classification" value="{{ art.Article_classification }}">
{% else %}
<input type="hidden" value="0" name="art_id">
<label><h2>文章标题</h2></label>
<input type="text" name="title">
<br>
<br>
<label><h2>文章内容</h2></label>
<input type="text" name="content">
<br>
<label><h3>文章作者</h3></label>
<input type="text" name="author" value="{{ art.author }}">
<br>
<label><h3>文章分类</h3></label>
<input type="text" name="classification" value="{{ art.Article_classification }}">
{% endif %}
<br>
<br>
<input type="submit" value="提交保存">
</form>
<a style="text-decoration-line: none" href="{% url 'blog:index' %}">返回主页</a>
</body>
</html>
3.3:index主页
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>欢迎来到我的博客</title>
</head>
<body>
<h1>我的博客列表</h1>
<br>
{% for art in articles %}
<a style="text-decoration-line: none" href="{% url 'blog:detail' art.id %}"><h2>{{ forloop.counter }} {{ art.title }}</h2></a>
{% endfor %}
<br>
<br>
<a style="text-decoration-line: none" href="{% url 'blog:edit' 0 %}">发布新文章</a>
</body>
</html>
4.运行截图:
主页:展示所有博客主题 详情页:展示博客内容 发布文章页面
后台截图:
按钮已经写好,可以发布文章,修改文章,删除文章等,可以进入后台进行数据的管理。
这只是简单的博客项目,会前端的同学可以将页面设计的更加美观,这里没有将前端页面进行美化,只是将数据展示在前端而已。这个项目可以进一步优化。目前具有博客的初级功能。以后开发,慢慢加。
注:使用的Django版本是1.几的,是软件自带的。版本比较低。高版本的Django在urls.py的路由配置的语法有些变化。注意即可。及以Path开头配置路由信息。