Django学习之路:编写博客

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})


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值