Django学习 (6):搭建简易博客

目录


原视频教程链接

django入门与实践(杜秉轩)


博客页面设计

  • 博客主页面
  • 博客文章内容页面
  • 博客撰写页面

博客主页面开发

页面内容

  • 文章标题列表,超链接
  • 发表博客按钮(超链接)

代码编写

1、修改blog/views.py中的index函数:

def index(request):
    articles = models.Article.objects.all()  # 获取所有文章对象列表
    return render(request, 'blog/index.html', {'articles': articles})  # 传递所有文章,作为参数

2、修改templates/blog下的index.html
添加:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Blog</title>
</head>
<body>
<h1>
    <a href="{#这里先空着#}">新文章</a>
</h1>
{#遍历articles中的所有文章,生成对应链接#}
{% for article in articles %}
    <a href="{#这里先空着#}">{{ article.title }}</a>
    <br>
{% endfor %}
</body>
</html>

博客文章页面开发

页面内容

  • 标题
  • 文章内容
  • 修改文章(超链接)

代码编写

1、在blog/views.py中添加article_page函数:

def article_page(request, article_id):  # 传入文章id
    article = models.Article.objects.get(pk=article_id)  # 根据文章id获取文章对象
    return render(request, 'blog/article_page.html', {'article': article})  # 传递文章对象

2、在blog/urls.py中添加:

url(r'^article/(?P<article_id>[0-9]+)$', views.article_page),    
# 获取article_id值,并传递给article_page函数

3、在templates/blog中添加模板article_page.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Article Page</title>
</head>
<body>
<h1>{{ article.title }}</h1>
<br>
<h3>{{ article.content }}</h3>
<br><br>
<a href="{#这里先空着#}">修改文章</a>
</body>
</html>

Django模板中的超链接配置

超链接配置使用Django的命名空间
命名空间-xiong的博客

超链接目标地址

  • href后面是目标地址
  • 模板中可以用"{% url 'app_name:url_name' param%}"作为地址
  • 其中app_nameurl_name都可以在url函数中设置

配置app_name

myblog/myblog/urls.py下修改:

    url(r'^blog/', include('blog.urls', namespace='blog')),

配置url_name

blog/urls.py下修改:

    url(r'^article/(?P<article_id>[0-9]+)$', views.article_page, name='article_page'),

在模板中使用

修改index.html

<a href="{% url 'blog:article_page' article.id %}">{{ article.title }}</a>

博客文章撰写页面

页面内容

  • 标题编辑栏
  • 文章内容编写区域
  • 提交按钮

小技巧

背景

  • html表单具有隐藏属性
  • 文章id从1开始

使用隐藏的input记录article_id

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

根据前端传来的article_id决定edit页面的显示方式

{% if article %}{# 有article传入,显示出原文章内容 #}
        <input type="hidden" name="article_id" value="{{ article.id }}">{# 隐藏表单存放article.id #}
        <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>
    {% else %}{# 没有article传入 #}
        <input type="hidden" name="article_id" value="0">{# 隐藏表单存放article.id,新建文章设置id为0#}

        <lable>文章标题
            <input type="text" name="title">
        </lable>
        <br>
        <lable>文章内容
            <input type="text" name="content">
        </lable>
        <br>
        <input type="submit" value="提交">
        </form>
    {% endif %}

根据article_id决定文章是新建还是修改

def edit_action(request):
    # 获取表单数据
    title = request.POST.get('title', 'TITLE')
    content = request.POST.get('content', 'CONTENT')
    article_id = request.POST.get('article_id', '0')
    if article_id == '0':  # 创建新文章
        models.Article.objects.create(title=title, content=content)
        return HttpResponseRedirect('/blog/index')
    # 修改文章
    article = models.Article.objects.get(pk=article_id)
    article.title = title
    article.content = content
    article.save()

    return HttpResponseRedirect('/blog/index')

代码编写

1、创建edit_page

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit Page</title>
</head>
<body>
<form action="{% url 'blog:edit_action' %}" method="post">
    {% csrf_token %}
    {% if article %}{# 有article传入,显示出原文章内容 #}
        <input type="hidden" name="article_id" value="{{ article.id }}">{# 隐藏表单存放article.id #}
        <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>
    {% else %}{# 没有article传入 #}
        <input type="hidden" name="article_id" value="0">{# 隐藏表单存放article.id,新建文章设置id为0#}

        <lable>文章标题
            <input type="text" name="title">
        </lable>
        <br>
        <lable>文章内容
            <input type="text" name="content">
        </lable>
        <br>
        <input type="submit" value="提交">
        </form>
    {% endif %}

</body>
</html>

2、在blog/views.py中添加:

def edit_page(request, article_id):
    if str(article_id) == '0':  # 如果article_id为0,即新建文章,不传参数
        return render(request, 'blog/edit_page.html')
    article = models.Article.objects.get(pk=article_id)  # 否则查找文章对象并传递
    return render(request, 'blog/edit_page.html', {'article': article})


def edit_action(request):
    # 获取表单数据
    title = request.POST.get('title', 'TITLE')
    content = request.POST.get('content', 'CONTENT')
    article_id = request.POST.get('article_id', '0')
    if article_id == '0':  # 创建新文章
        models.Article.objects.create(title=title, content=content)
        return HttpResponseRedirect('/blog/index')
    # 修改文章
    article = models.Article.objects.get(pk=article_id)
    article.title = title
    article.content = content
    article.save()

    return HttpResponseRedirect('/blog/index')

3、在blog/urls.py添加:

    url(r'^edit/(?P<article_id>[0-9]+)$', views.edit_page, name='edit_page'),
    url(r'^edit/action$', views.edit_action, name='edit_action'),

4、将之前空着的html中href属性填写好

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值