Edwin Rapheal
Edwin Rapheal

Reputation: 131

In Django Can i call a view function of App1 from url.py of App2?

App1: url.py, view.py App2: url.py view.py

Can i call a view function of App1 from url.py of App2 ?

Upvotes: 1

Views: 195

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476813

Yes, say that the views look like:

# app1/views.py

def view1(request):
    # ...
    pass

and

# app2/views.py

def view2(request):
    # ...
    pass

You can redirect to both views in the urls.py of an app, as long as you import it correctly:

# app1/urls.py

from django.urls import path

from app1.views import view1
from app2.views import view2

urlpatterns = [
    path('view1', view1),
    path('view2', view2),
]

So the app itself is not important, given you import the view function properly of course.

That being said, it is a bit uncommon to see this pattern. It is not impossible, and every now and then you see this. But typically the idea is that apps are not that much related. Sure some relations exist, but typically you aim to minimize this.

Upvotes: 2

Related Questions