Can we make another folder inside a view folder inside which we can keep a specific view file?


(PARTH JANI) #1

I used

Python 3.7
Django 2.0
My Project Name : fusion
My App Name : django_adminlte

issues is views normalization not working for views folder structure,I want Split views.py in several separated folder inside another separated folder due to lots of views files.

I already do this things with model but unfortunately cant work with views folder.i do all require file put in app .( i make__init__.py file also)

you assume that i want it like - example path dir
C:/project/app/views/sub_views1/views1.py
C:/project/app/views/sub_views1/views2.py
C:/project/app/views/sub_views2/views3.py
C:/project/app/views/sub_views2/views4.py

I want this type of setup for views file.

how can i do it ,which changes required in flow ?


(Vitor Freitas) #2

This is perfectly fine, you can keep this folder structure as you described, with views/sub_views, etc.

Then in your urls.py you simply import the views accordingly, like this:

from django.urls import path

from project.app.views.sub_views1 import views1
from project.app.views.sub_views1 import views2

urlpatterns = [
    path('homepage/', views1.homepage_view, name='homepage'),
    path('about/', views2.about_view, name='about'),
]

If you have name conflict you can rename the views module on import, like this:

from project.app.views.sub_views2 import views1 as sub_views2_views1

urlpatterns = [
    # ...
    path('privacy/', sub_views2_views1.privacy, name='privacy'),
]

In fact Django doesn’t really care where the view function/class is stored. All you have to do is to provide the link between the url and the view on your urls.py module

It’s a little bit different with models because Django expect to find your models inside the models.py file


(PARTH JANI) #3

Awesome its excellent working ,how can i forget it silly mistake,Thank you so much big bro vitor