Application specific middleware support

36 views
Skip to first unread message

Kapil Garg

unread,
Jul 7, 2019, 11:42:04 AM7/7/19
to Django developers (Contributions to Django itself)
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

Adam Johnson

unread,
Jul 7, 2019, 2:33:09 PM7/7/19
to django-d...@googlegroups.com
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 <kapilg...@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-develop...@googlegroups.com.
To post to this group, send email to django-d...@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/1b8a2339-50fd-4b81-973f-9474787e3c6e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Adam

Aymeric Augustin

unread,
Jul 7, 2019, 2:51:45 PM7/7/19
to django-d...@googlegroups.com
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 <kapilg...@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

Adam Johnson

unread,
Jul 7, 2019, 3:28:31 PM7/7/19
to django-d...@googlegroups.com
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".


For more options, visit https://groups.google.com/d/optout.


--
Adam

Aymeric Augustin

unread,
Jul 7, 2019, 3:41:09 PM7/7/19
to django-d...@googlegroups.com
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.



Reply all
Reply to author
Forward
0 new messages