V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
kangsgo
V2EX  ›  Python

Python 老司机带我开开车

  •  
  •   kangsgo · 2016-11-19 11:33:26 +08:00 · 3626 次点击
    这是一个创建于 2686 天前的主题,其中的信息可能已经有所发展或是发生改变。

    中间一大波重复的,有办法精简吗?

    def index(request):
        return HttpResponse("hello world")
    
    #软件页面
    def software(request):
        soft=SoftWare.objects.all()
        content = {'soft':soft}
        return render(request,'soft_index.html',content)
    
    #软件内容页面
    def software_detail(request,software_id):
        soft= SoftWare.objects.get(id=str(software_id))
        content = {'soft':soft}
        return render(request,'soft_detail.html',content)
    
    #博客首页
    def blog(request):
        post = Article.objects.all()
        content ={'post':post}
        return render(request,'blog_index.html',content)
    
    #博客内容页面
    def blog_detail(request,blog_id):
        post= Article.objects.get(id=str(blog_id))
        content = {'post':post}
        return render(request,'blog_detail.html',content)
    
    #脚本首页
    def bash(request):
        bash = Bash.objects.all()
        content ={'bash':bash}
        return render(request,'bash_index.html',content)
    
    #脚本内容页面
    def bash_detail(request,blog_id):
        bash= Article.objects.get(id=str(blog_id))
        content = {'bash':bash}
        return render(request,'bash_detail.html',content)
    
    #视频首页
    def video(request):
        post = Video.objects.all()
        content ={'video':video}
        return render(request,'video_index.html',content)
    
    #视频内容页面
    def video_detail(request,blog_id):
        post= Video.objects.get(id=str(blog_id))
        content = {'video':video}
        return render(request,'video_detail.html',content)
    
    16 条回复    2016-11-28 10:22:08 +08:00
    sfwn
        1
    sfwn  
       2016-11-19 11:56:20 +08:00   ❤️ 1
    software_id 和 blog_id 可以设默认值,然后方法里判断下
    kangsgo
        2
    kangsgo  
    OP
       2016-11-19 12:07:19 +08:00
    @sfwn 非常感谢!
    kangsgo
        3
    kangsgo  
    OP
       2016-11-19 12:37:33 +08:00
    @sfwn 开始以为搞懂了,发现又没懂了,能详细指导一下吗?
    Jblue
        4
    Jblue  
       2016-11-19 12:40:29 +08:00
    意思就是将 software_id 和 blog_id 可以区别开
    Daniel65536
        5
    Daniel65536  
       2016-11-19 13:40:20 +08:00 via iPad   ❤️ 3
    def page_factory(pagename, obj, template):
    ---- return lambda request: render(request, template, { pagename: obj.all() }

    video = page_factory( 'video', Video.objects, 'video_index.html')
    bash = page_factory( 'bash', Bash.objects, 'bash_index.html')
    nooper
        6
    nooper  
       2016-11-19 14:04:01 +08:00 via iPad   ❤️ 1
    你可以用 mixin
    jimzhong
        7
    jimzhong  
       2016-11-19 14:19:53 +08:00
    这样写 view 也没啥问题啊,只要性能不差,维护方便就好
    menc
        8
    menc  
       2016-11-19 14:21:54 +08:00   ❤️ 2
    @Daniel65536 像这个,就是典型的不可维护的代码,只关注炫技,完全不关注业务的内在联系
    wyntergreg
        9
    wyntergreg  
       2016-11-19 14:42:53 +08:00
    不需要精简
    srlp
        10
    srlp  
       2016-11-19 14:46:00 +08:00
    没什么好办法。

    我个人想法是,你非要简化的话可以用类来包装一下,然后用 override 实现多态。
    youyongsong
        11
    youyongsong  
       2016-11-19 14:53:50 +08:00
    看上去这些 view 并没有什么真正的业务逻辑,都是 model 驱动的普通的 CRUD ,这样的情况直接用 Django 的 generic views 就行不需要自己写 view 内容。
    zhuangzhuang1988
        12
    zhuangzhuang1988  
       2016-11-19 15:21:22 +08:00
    不需要的吧。。
    7sDream
        13
    7sDream  
       2016-11-19 15:24:07 +08:00
    generic views + 1
    reus
        14
    reus  
       2016-11-19 15:24:34 +08:00
    不需要精简,不然改动的时候你就知道烦了。
    rim99
        15
    rim99  
       2016-11-20 14:01:46 +08:00   ❤️ 1
    def fun(selector):
    ----'''selector 是 str 类型标识符
    ----dict_func = {selector : func, ... ...}
    ----dict_page = {selector : page, ... ...}
    ----content = {selector : SoftWare.objects.dict[selector]() }
    ----return render(request, dict_page[selctor], content)

    其他函数直接向 func 传递 selector 标识符就可以了
    t0p10
        16
    t0p10  
       2016-11-28 10:22:08 +08:00
    写的挺好的,改了反而难以读懂
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1122 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 22:50 · PVG 06:50 · LAX 15:50 · JFK 18:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.