一、表单验证
1.定义验证表单
from django import forms
class AddArtForm(forms.Form):
# required = True 表示必填项
# min_length 表示最小长度
title = forms.CharField(min_length=5, required=True)
desc = forms.CharField(min_length=20, required=True)
content = forms.CharField(required=True)
2.使用表单验证
is_valid():验证参数是否有效,如果参数验证成功返回True,否则返回False
from.errors:返回错误信息
from article.Artform import AddArtForm
def add_art(request):
if request.method == 'GET':
return render(request, 'add_article.html')
if request.method == 'POST':
# 把提交的数据交给表单AddArtForm做验证
form = AddArtForm(request.POST)
if form.is_valid():
title = form.cleaned_data['title']
desc = form.cleaned_data['desc']
content = form.cleaned_data['content']
Article.objects.create(title=title, desc=desc, content=content)
# 创建文章成功后,返回文章列表页面
return HttpResponseRedirect(reverse('art:art_list'))
else:
# 表示字段验证失败,需要将错误信息返回给页面展示
return render(request, 'add_article.html', {'form': form})
3.反向解析
(1)修改urls.py配置文件
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^article/', include('article.urls', namespace='art'))
]
(2)修改article.urls.py配置文件
from django.conf.urls import url
from article import views
urlpatterns = [
# 创建文章
url(r'^add_art/', views.add_art, name='add'),
# 文章列表
url(r'^art/', views.art, name='art_list'),
]
(3)url反向解析
from django.urls import reverse
def add_art(request):
if request.method == 'GET':
return render(request, 'add_article.html')
if request.method == 'POST':
form = AddArtForm(request.POST)
if form.is_valid():
title = form.cleaned_data['title']
desc = form.cleaned_data['desc']
content = form.cleaned_data['content']
Article.objects.create(title=title, desc=desc, content=content)
# url反向解析
return HttpResponseRedirect(reverse('art:art_list'))
else:
return render(request, 'add_article.html', {'form': form})
二、分页技术
1.Paginator分页技术
# 1.使用Paginator分页
p = Paginator(需要分页的总数据,一页的条数)
# 2.获取指定页码
page = p.page(获取哪一页的页码)
# 3.获取当前页
page.number
# 4.获取当前有多少页
page.p.num_pages
# 5.获取一共多少条数据
page.p.count
# 6.获取页码范围
page.p.page_range
# 7.是否存在上一页
page.has_previous
# 8.是否存在下一页
page.has_next
# 9.获取上一页页码
page.previous_page_number
# 10.获取下一页页码
page.next_page_number
2.使用Paginator进行分页
from django.core.paginator import Paginator
def art(request):
if request.method == 'GET':
# 文章列表页面
page_num = int(request.GET.get('page', 1))
# 使用Paginator进行分页
articles = Article.objects.all()
# 将所有数据按照每一页2条数据进行切块处理
paginator = Paginator(articles, 2)
# 获取分页中的第几页数据
page = paginator.page(page_num)
return render(request, 'art.html', {'page': page})
3.使用切片进行分页
def art(request):
if request.method == 'GET':
# 文章列表页面
page_num = int(request.GET.get('page', 1))
# 使用切片完成分页
articles = Article.objects.all()[(page-1)*2: page*2]
return render(request, 'art.html', {'page': page})
三、图片上传
1.表单上传图片
注意:表单中必须有enctype="multipart/form-data"
<form action="" method="post" enctype="multipart/form-data">
<p>图片:<input type="file" name="icon"></p>
<input type="submit" value="提交">
</form>
2.修改setting.py配置文件
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
# 媒体文件media配置
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
3.在工程目录的urls.py文件中配置解析media的路由
from django.contrib.staticfiles.urls import static
from day07.settings import MEDIA_URL, MEDIA_ROOT
# 告诉Django,解析media文件夹中的静态文件
urlpatterns += static(MEDIA_URL, document_root=MEDIA_ROOT)
4.模型中定义保存图片的字段
models.ImageFiled(upload_to='article')
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=100)
desc = models.CharField(max_length=150)
content = models.TextField()
icon = models.ImageField(upload_to='article', null=True)
create_time = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = 'article'
5.定义视图函数
获取图片:request.FILES
def add_art(request):
if request.method == 'GET':
return render(request, 'add_article.html')
if request.method == 'POST':
form = AddArtForm(request.POST, request.FILES)
if form.is_valid():
title = form.cleaned_data['title']
desc = form.cleaned_data['desc']
content = form.cleaned_data['content']
icon = form.cleaned_data['icon']
Article.objects.create(title=title, desc=desc, content=content, icon=icon)
return HttpResponseRedirect(reverse('art:art_list'))
else:
return render(request, 'add_article.html', {'form': form})
6.页面解析图片
页面解析图片<img src = "/media/{{ 图片对象.icon }}">
{% for art in page %}
<tr>
<td>{{ art.id }}</td>
<td>{{ art.title }}</td>
<td>{{ art.desc }}</td>
<td>{{ art.content }}</td>
<td><img src="/media/{{ art.icon }}" width=100px; height=100px></td>
</tr>