python_django_web开发:10 编辑页面的两种来路处理与博客小项目源码

本文介绍了一个简单的Django博客系统的实现过程,包括视图、URL配置及HTML模板的使用。重点讲解了如何通过视图处理请求并展示文章列表、文章详情及文章编辑功能。

views.py

from django.shortcuts import render
from . import models
from django.http import  HttpResponse
# Create your views here.

# def index(request):
#     return HttpResponse("hello sxs")


def index(request):

    articles = models.Article.objects.all()
    return render(request, 'blog/index.html', {'articles': articles})


def article_page(request, article_id):

    article = models.Article.objects.get(pk=article_id)
    return render(request, 'blog/article_page.html', {'article': article})


def article_edit_page(request, article_id):

    if str(article_id) == '0':
        return render(request, 'blog/article_edit_page.html')
    article = models.Article.objects.get(pk=article_id)
    return render(request, 'blog/article_edit_page.html', {'article': article})


def article_edit_page_action(request):
    title = request.POST.get('title', '默认标题')
    content = request.POST.get('content', '默认内容')
    article_id_hide = request.POST.get('article_id_hide', '0')
    articles = models.Article.objects.all()
    if article_id_hide == '0':
        models.Article.objects.create(title=title, content=content)
        return render(request, 'blog/index.html', {'articles': articles})

    article = models.Article.objects.get(pk=article_id_hide)
    article.title = title
    article.content = content
    article.save()
    return render(request, 'blog/article_page.html', {'article': article})

url.py

from . import views
from django.conf.urls import url


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

article_edit_page.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>博客的编辑页面</title>
</head>
<body>
<form action="{% url 'blog:article_edit_page_action' %}" method="post">
    {% csrf_token %}

    {% if article %}
        <input type="hidden" name="article_id_hide" value={{ article.id }}>
        <label>博客标题
            <input type="text" name="title" value="{{ article.title }}">
        </label>
        <br>
        <label>博客内容
            <input type="text" name="content" value="{{ article.content }}">
        </label>
        <br>
    {% else %}
        <input type="hidden" name="article_id_hide" value="0">

        <label>博客标题
            <input type="text" name="title">
        </label>
        <br>
        <label>博客内容
            <input type="text" name="content">
        </label>
        <br>
    {% endif %}

    <input type="submit" name="提交">
</form>
</body>
</html>

article_page.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>这是第二个页面</title>
</head>
<body>
<h2>{{article.title}}</h2>
<h2>{{ article.content }}</h2>


<a href="{% url 'blog:article_edit_page' article.id %}" >编辑</a>
</body>
</html>

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1></h1>
<h1>这是首页</h1>

{% for article in articles %}
    <a href = "{% url 'blog:article' article.id %}">{{ article.title}}</a>
    <br>
{% endfor %}
<br>
<br>
<a href="{% url 'blog:article_edit_page' 0 %}">写新博客</a>
</body>
</html>

学习过程中遇到的问题,主要是语法书写错误

在views.py文件中

render(request, 'blog/article_edit_page.html', {'article': article})

错写成
render(request, 'blog/article_edit_page.html', {'article', article})
article = models.Article.objects.get(pk=article_id_hide)
错写成:
article = models.Article.objects.get(article_id_hide)

Python实现一些小道具小功能(Python implements some small props) Image-Edit 几个基本的图片编辑工具,包括一下功能: 文件:打开,保存,退出 编辑:放大,缩小,灰度,亮度,旋转,截图 变换:傅里叶变换,离散余弦变换,Radon变换 噪声:高斯,椒盐,斑点,泊松 滤波:高通,低通,平滑,锐化 直方图统计:R直方图,G直方图,B直方图 图像增强:伪彩色,真彩色,直方图均衡,NTSC颜色模型,YCbCr颜色模型,HSV颜色模型 阈值分割 生态学处理 特征提取 图像分类识别 Beautify-Camera 主要功能 文件:打开,保存,打开摄像头 操作:还原,人脸识别 滤镜:怀旧,木刻,灰色,彩色,风格化,增强细节 调节:亮度,饱和度,伽马变换,边缘保持 磨皮美白:美白度,磨皮程度,磨皮精度 灰度直方图 Calculator 主要功能 基本的加减乘除和开根号等运算 Painting绘画 主要功能 File:新建画板,打开图片,保存图片 Edit:复制,清空画板 Image:翻转 工具:基本画笔,橡皮擦,图形创建工具等 编辑区,色彩调节区,字体调节区等 NotePad 主要功能 基本文本编辑,类似于记事本 RandomPassWord 主要功能 随机生成一串密码,包括大小写字母,数字,符号,可指定长度 Browser 主要功能 基本浏览器功能 MusicPlayer 主要功能 音乐播放器 PyTunes 主要功能 轻量级音乐播放器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值