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)

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

被折叠的 条评论
为什么被折叠?



