Application specific middleware support

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Application specific middleware support

Kapil Garg-2
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 [hidden email].
To post to this group, send email to [hidden email].
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/1b8a2339-50fd-4b81-973f-9474787e3c6e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Application specific middleware support

Adam Johnson-2
Hi Kapil

The main reason I believe is because URL's are project global, rather than per app. Yes the root urlconf can include URL's kept within apps, but because it's a recursive data structure this isn't so easy.

For your use case I'd suggest either:
  • Using class based views, creating a subclass of View in your app with your middleware-like behaviour in its dispatch() method, and using that subclass in all your app's views
  • or similarly using a view decorator and ensuring it is applied to all views in your app
The final option is to use the middleware process_view method and inspect if the view lives within the target app.

Hope that helps,

Adam

On Sun, 7 Jul 2019 at 16:42, Kapil Garg <[hidden email]> 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 [hidden email].
To post to this group, send email to [hidden email].
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/1b8a2339-50fd-4b81-973f-9474787e3c6e%40googlegroups.com.
For more options, visit 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 [hidden email].
To post to this group, send email to [hidden email].
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/CAMyDDM1dWE6u%2BEggfNbgPQ_m3P9T-H6BKzjm-8qvbDXx9YW-%2Bg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Application specific middleware support

Aymeric Augustin
In reply to this post by Kapil Garg-2
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 <[hidden email]> 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 [hidden email].
To post to this group, send email to [hidden email].
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/1b8a2339-50fd-4b81-973f-9474787e3c6e%40googlegroups.com.
For more options, visit 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 [hidden email].
To post to this group, send email to [hidden email].
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/DEB7E81B-0043-4754-9D23-A6A234F9BCC6%40polytechnique.org.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Application specific middleware support

Adam Johnson-2
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

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 <[hidden email]> 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 <[hidden email]> 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 [hidden email].
To post to this group, send email to [hidden email].
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/1b8a2339-50fd-4b81-973f-9474787e3c6e%40googlegroups.com.
For more options, visit 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 [hidden email].
To post to this group, send email to [hidden email].
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/DEB7E81B-0043-4754-9D23-A6A234F9BCC6%40polytechnique.org.
For more options, visit 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 [hidden email].
To post to this group, send email to [hidden email].
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/CAMyDDM3-B49mPzRT_d2bGU-6MOxiJ%3DAt%2BqYaR0ifgAEhOQiFzQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Application specific middleware support

Aymeric Augustin
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 <[hidden email]> 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

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 <[hidden email]> 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 <[hidden email]> 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 [hidden email].
To post to this group, send email to [hidden email].
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/1b8a2339-50fd-4b81-973f-9474787e3c6e%40googlegroups.com.
For more options, visit 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 [hidden email].
To post to this group, send email to [hidden email].
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/DEB7E81B-0043-4754-9D23-A6A234F9BCC6%40polytechnique.org.
For more options, visit 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 [hidden email].
To post to this group, send email to [hidden email].
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/CAMyDDM3-B49mPzRT_d2bGU-6MOxiJ%3DAt%2BqYaR0ifgAEhOQiFzQ%40mail.gmail.com.
For more options, visit 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 [hidden email].
To post to this group, send email to [hidden email].
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.