|
|
from django.shortcuts import render, redirect
|
|
|
from app01 import models
|
|
|
|
|
|
from app01.utils.pagination import Pagination
|
|
|
from app01.utils.form import UserModelForm, PrettyModelForm, PrettyEditModelForm,MovieModelForm
|
|
|
|
|
|
def movie_list(request):
|
|
|
data_dict = {}
|
|
|
search_data = request.GET.get('q', "")
|
|
|
if search_data:
|
|
|
data_dict["name__contains"] = search_data
|
|
|
|
|
|
queryset = models.Movie.objects.filter(**data_dict)
|
|
|
|
|
|
page_object = Pagination(request, queryset, page_size=6)
|
|
|
context = {
|
|
|
"queryset": page_object.page_queryset,
|
|
|
"page_string": page_object.html(),
|
|
|
'search_data': search_data,
|
|
|
}
|
|
|
return render(request,'movie_list.html',context)
|
|
|
|
|
|
|
|
|
def movie_add(request):
|
|
|
if request.method =='GET':
|
|
|
form = MovieModelForm()
|
|
|
return render(request,'movie_add.html',{'form':form})
|
|
|
form = MovieModelForm(data = request.POST)
|
|
|
if form.is_valid():
|
|
|
form.save()
|
|
|
return redirect('/movie/list/')
|
|
|
|
|
|
return render(request,'movie_add.html',{'form':form})
|
|
|
|
|
|
|
|
|
def movie_edit(request,nid):
|
|
|
row_object = models.Movie.objects.filter(id=nid).first()
|
|
|
|
|
|
if request.method == "GET":
|
|
|
# 根据ID去数据库获取要编辑的那一行数据(对象)
|
|
|
form = MovieModelForm(instance=row_object)
|
|
|
return render(request, 'movie_add.html', {'form': form})
|
|
|
|
|
|
form = MovieModelForm(data=request.POST, instance=row_object)
|
|
|
if form.is_valid():
|
|
|
# 默认保存的是用户输入的所有数据,如果想要再用户输入以外增加一点值
|
|
|
# form.instance.字段名 = 值
|
|
|
form.save()
|
|
|
return redirect('/movie/list/')
|
|
|
return render(request,'movie_edit.html',{'form':form})
|
|
|
|
|
|
def movie_delete(request, nid):
|
|
|
models.Movie.objects.filter(id=nid).delete()
|
|
|
return redirect('/movie/list/') |