·

How to split views.py to several smaller files in Django

Published at 2024-05-01 14:11:30Viewed 317 times
Professional article
Please reprint with source link

When your views.py is too large, it becomes much more difficult to manage your code. So you might want to split views.py into several subviews, each of which contains a single sort of views.

To split views.py, you could follow the following steps. Your original views.py might look like this:

import ...

def view1(request):
    pass

def view2(request):
   pass

Now, create a new folder subviews aside with views.py , i.e. /Django/mysite/MyAPI/subviews. Next, create __init__.py, viewsa.py, and viewsb.py in the folder subviews to form the following folder/file structure :

subviews/
   __init__.py
   subviews1.py
   subviews2.py

subviews1.py :

from .views import *

def view1(request):
    pass

subviews2.py :

from .views import *

def view2(request):
    pass

__init__.py :

from subviews1 import *
from subviews2 import *

Then this would work the same as a single views.py file. Don't forget to change the urls.py file before you reloading Django.

Comments

There is no comment, let's add the first one.

弦圈热门内容

Grothendick经典同调代数文章:Some aspects of homological algebra

这是Grothendick著名的关于同调代数的文章Tôhoku paper的英文翻译版,原文是法语版,标题为Sur quelques points d'algèbre homologique。英文翻译为:Some aspects of homological algebra。该文章概述了很多同调代数的重要概念,其中基本都跟代数几何有联系,并且里面不少概念其实是Grothendick本人提出来的,如abelian categories。可以说这篇文章是同调代数的经典文章,在数学圈内也时常有人推荐看这篇文章,毕竟这可是祖师爷亲自从同调代数的基础概念一步步讲起,这对学同调代数或者代数几何的人都有很大裨益。我收藏这篇文章的时候都2021年了,现在拿出来推荐给大家!之后我还会把法语原版也发出来。

Get connected with us on social networks! Twitter

©2024 Guangzhou Sinephony Technology Co., Ltd All Rights Reserved