Django运行的顺序:1.网站打开url,运行urls.py文件;
2.urls.py里面运行对应的views.py里面的文件;
3.view.py里面对应的模块返回templates/里面的html文件
过滤器简介地址:https://blog.csdn.net/foryouslgme/article/details/52057445?utm_source=blogxgwz4
编辑博客主页html:
/blog/article/{{ article.id }} 跳转到对应的博客详情页,加/代表绝对路径,没有就是相对路径
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--<h1>{{ article.title }}</h1>-->
<!--<h3>{{article.content}}</h3>-->
<h1><a href="/blog/edit/0">新文章</a>
</h1>
{% for article in articles %}
{% csrf_token %}
<a href="/blog/article/{{ article.id }}">{{ article.title }}</a>
<br>
{% endfor %}
</body>
</html>
博客详情页html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>{{ article.title }}</h1>
<br>
<h4>{{ article.content }}</h4>
<br>
<a href="/blog/edit/{{ article.id }}">修改文章</a>
</body>
</html>
博客编辑页html:
过滤器{{ article.id |default:"0"}}默认为0
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/blog/edit/action" method="post">
{% csrf_token %}
<input type="hidden" name="article_id" value="{{ article.id |default:"0"}}">
<lable>文章标题
<input type=text" name="title" value="{{ article.title }}"/>
</lable>
<br/>
<lable>
文章内容
<input type="text" name="content" value="{{ article.content }}"/>
</lable>
<br/>
<input type="submit" value="提交">
</form>
</body>
</html>
urls.py配置:
<int:article_id>传INT类型参数
urlpatterns = [
path('index/', views.index),
path('article/<int:article_id>',views.article_page),
path('edit/<int:article_id>',views.edit_page),
path('edit/action',views.edit_action)
]
重点views.py配置:
from django.http import HttpResponse
from django.shortcuts import render
from blog import models
#博客主页 path('index/', views.index),
def index(request):
articles=models.Article.objects.all()
return render(request,'index.html',{'articles':articles})
#博客详情页 path('article/<int:article_id>',views.article_page),
def article_page(request,article_id):
article=models.Article.objects.get(pk=article_id)
return render(request,'article_page.html',{'article':article})
#编辑博客 path('edit/<int:article_id>',views.edit_page),
def edit_page(request,article_id):
#如果article_id=0,新建博客,否则修改博客
if str(article_id)=='0':
return render(request,'edit_page.html')
article=models.Article.objects.get(pk=article_id)
return render(request,'edit_page.html',{'article':article})
# 修改新增博客 path('edit/action',views.edit_action)
def edit_action(request):
title=request.POST.get('title','TITLE')
content=request.POST.get('content','CONTENT')
article_id=request.POST.get('article_id')
#新建博客,返回主页
if str(article_id) == '0':
models.Article.objects.create(title=title,content=content)
#获取所有的数据
articles = models.Article.objects.all()
#修改博客,返回修改后博客
return render(request, 'index.html', {'articles': articles})
#获取pk=article_id的数据
article=models.Article.objects.get(pk=article_id)
#保存修改的博客
article.title=title
article.content=content
article.save()
#返回到博客主页
return render(request, 'article_page.html', {'article': article})