Split views.py in several files

前端 未结 10 854
有刺的猬
有刺的猬 2020-11-27 09:37

My views.py has become too big and it\'s hard to find the right view.

How do I split it in several files and then import it? Does it involve any speed l

10条回答
  •  佛祖请我去吃肉
    2020-11-27 10:02

    I've had to do this before (for clarities sake)

    The way I did this was to create a views directory, then, in that, create a file called __init__.py

    Now, when you're calling in your urls.py, you simply need to add another part

    For example, previously, you may have called:-

    url(r'^calendar/(?P\d\d\d\d)/$', 'myproject.calendar.views.year')
    url(r'^calendar/(?P\d\d\d\d)/(?P[a-z]+)/$', 'myproject.calendar.views.year_by_user')
    

    You can now call something along the lines of

    url(r'^calendar/(?P\d\d\d\d)/$', 'myproject.calendar.views.year.index')
    url(r'^calendar/(?P\d\d\d\d)/(?P[a-z]+)/$', 'myproject.calendar.views.year.user')
    

    This is, of course, assuming that you had views/year.py containing the functions index and user ;)

提交回复
热议问题