Split views.py in several files
In Django everything is a Python module (*.py). You can create a view folder with an __init__.py
inside and you still will be able to import your views, because this also implements a Python module. But an example would be better.
Your original views.py
might look like this :
def view1(arg):
pass
def view2(arg):
pass
With the following folder/file structure it will work the same :
views/
__init__.py
viewsa.py
viewsb.py
viewsa.py
:
def view1(arg):
pass
viewsb.py
:
def view2(arg):
pass
__init__.py
:
from viewsa import view1
from viewsb import view2
The quick explanation would be: when you write from views import view1
Python will look for view1 in
views.py
, which is what happens in the first (original) caseviews/__init__.py
, which is what happens in the second case. Here,__init__.py
is able to provide the view1 method because it imports it.
With this kind of solution, you might have no need to change import
or urlpattern
s arguments in urls.py
If you have many methods in each new view file, you might find it useful to make the imports in views/__init__.py
use *
, like this:
from viewsa import *
from viewsb import *
I actually don't know about speed issues (but I doubt there are any).
For Models it might be a bit difficult.
Can we create multiple views in django
Usually there's exactly one views.py
file per app, but this is just by convention. views.py
is just a regular python file and you can use any name you want. (Make sure to update to imports in the urls.py
).
I'd recommend creating a new module called views
to collect multiple views (adminViews.py, userviews.py etc)
Note: A python module is just a folder with an __init__py
file in it.
Your new folder structure might look like this:
You can import your views like this:
# urls.py
from .views.adminViews import YourAdminView
split views.py into multiple files
Write your code in urls.py
as normal (i.e., import the view from that views
module as if it were views.py
) and add to __init__.py
within views
:
from sign_in_out_up import *
This ensures you can continue to split up views in the views
module without having to update urls.py
all the time.
Django - split view.py to small files
Yes, there is not much special about views.py
itself. You can implement two files for example:
# app/views_simpel.py
def view1(request):
# ...
pass
def view2(request):
# ...
pass
and another one:
# app/views_complex.py
def view3(request):
# ...
pass
def view4(request):
# ...
pass
In your urls.py
you can then import those views, for example:
# app/urls.py
from django.urls import path
from app.views_simpel import view1, view2
from app.views_complex import view3, view4
urlpatterns = [
path('view1/', view1),
path('view2/', view2),
path('view3/', view3),
path('view4/', view4),
]
Both files can contain function-based views, class-based views, etc. In fact, the urls.py
does not see much difference between the two, since by using .as_view()
on a class-based view, you hand it a "dispatcher" function.
Django - split views into separate files while maintaining views.py
The direct answer is yes, you have a python naming collision. See for example Python Import Class With Same Name as Directory
You don't need to do it all as once though -- you can simply rename / move your views.py file or your new views directory. Moving the new directory would likely be easiest, then you won't have change your existing url routes.
There's nothing special about views.py files, as long as your urls.py points to the appropriate function, you can place them anywhere, and call them anything.
Split models.py into several files
I'd do the following:
myproject/
...
app1/
views.py
__init__.py
models.py
submodels/
__init__.py
model1.py
model2.py
app2/
views.py
__init__.py
models.py
submodels/
__init__.py
model3.py
model4.py
Then
#myproject/app1/models.py:
from submodels/model1.py import *
from submodels/model2.py import *
#myproject/app2/models.py:
from submodels/model3.py import *
from submodels/model4.py import *
But, if you don't have a good reason, put model1 and model2 directly in app1/models.py and model3 and model4 in app2/models.py
---second part---
This is app1/submodels/model1.py file:
from django.db import models
class Store(models.Model):
class Meta:
app_label = "store"
Thus correct your model3.py file:
from django.db import models
from app1.models import Store
class Product(models.Model):
store = models.ForeignKey(Store)
class Meta:
app_label = "product"
Edited, in case this comes up again for someone:
Check out django-schedule for an example of a project that does just this.
https://github.com/thauber/django-schedule/tree/master/schedule/models
https://github.com/thauber/django-schedule/
Split django models.py into multiple files inside folder (Django 3.0.4)
You can do that putting them into a models folder, like:
models/
- __init__.py
- model_1.py
- model_2.py
and __init__.py
should import all models contained in the other files
from .model_1 import Model1
from .model_2 import Model2
It is up to you to split them, depending on if you have a lot of models and if those are stricly related to each others.
Edit:
inside init.py
from .post import Post
from .like import Like
from .profile import Profile
from .comment import Comment
inside comment.py
from django.db import models
from django.conf import settings
from test1.models import Post
class Comment(models.Model):
commented_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
for_post = models.ForeignKey(Post, on_delete=models.CASCADE)
inside like.py
from django.db import models
from django.conf import settings
from test1.models import Post
class Like(models.Model):
liked_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
inside post.py
from django.db import models
from django.conf import settings
class Post(models.Model):
posted_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
inside profile.py
from django.db import models
from django.conf import settings
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
Can't split django views into subfolders
Use relative import, in your __init__.py
:
from .viewsa import *
(notice the dot in .viewsa
)
ImportError: No module named models after split views.py into several files
Without knowing your directory structure it's not possible to give you an exact answer but you almost definitely moved your newly split apart views.py
into a views/
directory which means that the relative reference has been broken. Try this
from ..models import MyModel
which means go one directory up the tree.
Also, make sure that there's an __init__.py
file in your views/
directory. Fine if it's blank.
Related Topics
Why Do I Need to Deploy a "Default" App Before I Can Deploy Multiple Services in Gae
Monkey Patching a Class in Another Module in Python
How to Display a 3D Plot of a 3D Array Isosurface in Matplotlib Mplot3D or Similar
Browse Files and Subfolders in Python
Reading the Target of a .Lnk File in Python
Difference Between Data and JSON Parameters in Python Requests Package
How to Send an Xml Body Using Requests Library
Filtering a List Based on a List of Booleans
How to Clone a Python Generator Object
Import Script from a Parent Directory
Flask-Sqlalchemy Update a Row's Information
Crawling with an Authenticated Session in Scrapy
Importerror: No Module Named 'Django.Core.Urlresolvers'