- FBV
# 1
def add_publisher(request):
error_msg = ""
# 如果是POST请求,我就取到用户填写的数据
if request.method == "POST":
new_name = request.POST.get("publisher_name", None)
if new_name:
# 通过ORM去数据库里新建一条记录
models.Publisher.objects.create(name=new_name)
# 引导用户访问出版社列表页,查看是否添加成功 --> 跳转
return redirect("/publisher_list/")
else:
error_msg = "出版社名字不能为空!"
# 用户第一次来,我给他返回一个用来填写的HTML页面
return render(request, "add_publisher.html", {"error": error_msg})
# 2
url(r'^publisher_list/', views.publisher_list),
- CBV
# 1
from django.views import View
# CBV版 添加新的出版社
class AddPublisher(View):
def get(self, request):
print(request.path_info)
print(request.body)
print("=" * 120)
return render(request, "add_publisher.html")
def post(self, request):
print(request.body)
print("=" * 120)
new_name = request.POST.get("publisher_name", None)
if new_name:
# 通过ORM去数据库里新建一条记录
models.Publisher.objects.create(name=new_name)
# 引导用户访问出版社列表页,查看是否添加成功 --> 跳转
return redirect("/publisher_list/")
else:
error_msg = "出版社名字不能为空!"
return render(request, "add_publisher.html", {"error": error_msg})
# 2
url(r'^publisher_list2/', views.reportInfoListView.as_view()),