Oops! Scratch that, let's try again... So Django doesn't create a relationship incoming HTTP requests and applications. You could try to create such a relationship, based on where URLconfs or views are defined, but you'll quickly discover tons of edge cases — starting with decorators — that make this unreliable in practice. You'd be going against Django's #1 design philosophy: loose coupling.
If you have control over the project and you know where the URLconf for the app is included, you can do: `if request.path.startswith("/your_app/"): do_stuff()`. If you're building a pluggable app and you don't have control over the project, you can write a decorator and apply it to every view. Best regards, -- Aymeric. > On 7 Jul 2019, at 21:28, Adam Johnson <m...@adamj.eu> wrote: > > Aymeric, are you sure about that? I can't find the code, and the docs for > current_app say it's noto set by Django itself: > https://docs.djangoproject.com/en/2.2/ref/request-response/#django.http.HttpRequest.current_app > > <https://docs.djangoproject.com/en/2.2/ref/request-response/#django.http.HttpRequest.current_app> > > Also middleware can't reliably make use of routing information except in > process_view, because if another middleware changes request.urlconf, it will > be "re-routed". > > On Sun, 7 Jul 2019 at 19:51, Aymeric Augustin > <aymeric.augus...@polytechnique.org > <mailto:aymeric.augus...@polytechnique.org>> wrote: > Hello Kapil, > > When you talk of a "URL served by an app", I suppose you're referring to URL > namespaces, which provide a request.currrent_app attribute associating an > HTTP request to an app. As far as I know, this is the only association of a > request and an app that Django defines. > > Django's documentation encourages URL namespaces for all pluggable apps. > Unfortunately, adoption is limited because the benefits aren't sufficiently > clear for many users. That said, if you need to add a middleware to an app > that's under your control, you can give this app a URL namespace. Once you've > done that, in your middleware, you can run your logic only if the request is > routed to your app: `if request.current_app == "your_app": do_stuff()`. > > As to why this isn't documented, most likely, that's because nobody ever had > this use case and took the time to submit a documentation patch. That's a > good contribution opportunity :-) > > Best regards, > > -- > Aymeric. > > > >> On 7 Jul 2019, at 17:42, Kapil Garg <kapilgarg1...@gmail.com >> <mailto:kapilgarg1...@gmail.com>> wrote: >> >> Hi, I am working on one django project and recently i had the requirement >> for a middleware. But i don't want this middleware to hook to every url >> served by the whole project but instead, only to one of the apps. I tried to >> look on the internet but everywhere there are hacks to implement it but not >> a in-built support from django. So i was curious why django doesn't provide >> support for app specific middleware or if it does then why there isn't any >> neat documentation about it. Thanks >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django developers (Contributions to Django itself)" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to django-developers+unsubscr...@googlegroups.com >> <mailto:django-developers+unsubscr...@googlegroups.com>. >> To post to this group, send email to django-developers@googlegroups.com >> <mailto:django-developers@googlegroups.com>. >> Visit this group at https://groups.google.com/group/django-developers >> <https://groups.google.com/group/django-developers>. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-developers/1b8a2339-50fd-4b81-973f-9474787e3c6e%40googlegroups.com >> >> <https://groups.google.com/d/msgid/django-developers/1b8a2339-50fd-4b81-973f-9474787e3c6e%40googlegroups.com?utm_medium=email&utm_source=footer>. >> For more options, visit https://groups.google.com/d/optout >> <https://groups.google.com/d/optout>. > > > -- > You received this message because you are subscribed to the Google Groups > "Django developers (Contributions to Django itself)" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-developers+unsubscr...@googlegroups.com > <mailto:django-developers+unsubscr...@googlegroups.com>. > To post to this group, send email to django-developers@googlegroups.com > <mailto:django-developers@googlegroups.com>. > Visit this group at https://groups.google.com/group/django-developers > <https://groups.google.com/group/django-developers>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-developers/DEB7E81B-0043-4754-9D23-A6A234F9BCC6%40polytechnique.org > > <https://groups.google.com/d/msgid/django-developers/DEB7E81B-0043-4754-9D23-A6A234F9BCC6%40polytechnique.org?utm_medium=email&utm_source=footer>. > For more options, visit https://groups.google.com/d/optout > <https://groups.google.com/d/optout>. > > > -- > Adam > > -- > You received this message because you are subscribed to the Google Groups > "Django developers (Contributions to Django itself)" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-developers+unsubscr...@googlegroups.com > <mailto:django-developers+unsubscr...@googlegroups.com>. > To post to this group, send email to django-developers@googlegroups.com > <mailto:django-developers@googlegroups.com>. > Visit this group at https://groups.google.com/group/django-developers > <https://groups.google.com/group/django-developers>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-developers/CAMyDDM3-B49mPzRT_d2bGU-6MOxiJ%3DAt%2BqYaR0ifgAEhOQiFzQ%40mail.gmail.com > > <https://groups.google.com/d/msgid/django-developers/CAMyDDM3-B49mPzRT_d2bGU-6MOxiJ%3DAt%2BqYaR0ifgAEhOQiFzQ%40mail.gmail.com?utm_medium=email&utm_source=footer>. > For more options, visit https://groups.google.com/d/optout > <https://groups.google.com/d/optout>. -- You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To post to this group, send email to django-developers@googlegroups.com. Visit this group at https://groups.google.com/group/django-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/6FEAAD9C-0D49-4D34-85C4-71100CD48841%40polytechnique.org. For more options, visit https://groups.google.com/d/optout.