全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 2228|回复: 11

python老司机带带我

[复制链接]
发表于 2016-11-19 12:29:03 | 显示全部楼层 |阅读模式
给老板做网站,这中间一大段有办法精简吗?

  1. def index(request):
  2.     return HttpResponse("hello world")

  3. #软件页面
  4. def software(request):
  5.     soft=SoftWare.objects.all()
  6.     content = {'soft':soft}
  7.     return render(request,'soft_index.html',content)

  8. #软件内容页面
  9. def software_detail(request,software_id):
  10.     soft= SoftWare.objects.get(id=str(software_id))
  11.     content = {'soft':soft}
  12.     return render(request,'soft_detail.html',content)

  13. #博客首页
  14. def blog(request):
  15.     post = Article.objects.all()
  16.     content ={'post':post}
  17.     return render(request,'blog_index.html',content)

  18. #博客内容页面
  19. def blog_detail(request,blog_id):
  20.     post= Article.objects.get(id=str(blog_id))
  21.     content = {'post':post}
  22.     return render(request,'blog_detail.html',content)

  23. #脚本首页
  24. def bash(request):
  25.     bash = Bash.objects.all()
  26.     content ={'bash':bash}
  27.     return render(request,'bash_index.html',content)

  28. #脚本内容页面
  29. def bash_detail(request,blog_id):
  30.     bash= Article.objects.get(id=str(blog_id))
  31.     content = {'bash':bash}
  32.     return render(request,'bash_detail.html',content)

  33. #视频首页
  34. def video(request):
  35.     post = Video.objects.all()
  36.     content ={'video':video}
  37.     return render(request,'video_index.html',content)

  38. #视频内容页面
  39. def video_detail(request,blog_id):
  40.     post= Video.objects.get(id=str(blog_id))
  41.     content = {'video':video}
  42.     return render(request,'video_detail.html',content)
复制代码
发表于 2016-11-19 12:33:39 | 显示全部楼层
老板估计不懂python,能用就行了。
 楼主| 发表于 2016-11-19 12:34:46 | 显示全部楼层

恩恩,是的,好多重复代码,看的不爽
 楼主| 发表于 2016-11-19 12:36:07 | 显示全部楼层
倾城翻翻 发表于 2016-11-19 12:33
老板估计不懂python,能用就行了。

重复代码太多,看的不爽啊,我有洁癖
发表于 2016-11-19 12:37:43 | 显示全部楼层
都是重复代码可以写个装饰器
 楼主| 发表于 2016-11-19 12:40:32 | 显示全部楼层
tumbzzc 发表于 2016-11-19 12:37
都是重复代码可以写个装饰器

能否详细指导一下?
发表于 2016-11-19 12:48:45 | 显示全部楼层
kangsgo 发表于 2016-11-19 12:40
能否详细指导一下?

def add(x, y):
     return x + y
def sub(x, y):
     return x - y
def apply(func, x, y): # 1
     return func(x, y) # 2
apply(add, 2, 1) # 3
3
参照这个例子
发表于 2016-11-19 12:50:10 | 显示全部楼层
generic.DetailView

自己抄文档吧
发表于 2016-11-19 12:50:56 | 显示全部楼层
老司机翻车了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-5-31 12:28 , Processed in 0.132196 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表