1

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

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

3
  • 1
    Yes, why not? As long as you import it. Commented Oct 8, 2018 at 11:54
  • Yes you can ! What is the issue ? Commented Oct 8, 2018 at 11:54
  • yes but why would you do that, bring that view function to the app where the url is. Also why two urls for same thing. But still if you want to do that don't call that view function in another app, redirect that app2 url to the url of app1 which uses that view function. Commented Oct 8, 2018 at 11:57

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.