Package: django-url-prefixer
Django URL PrefixerA Django middleware that prepends some text/link to every relative link before rendering a template.Therefore, relative link inside a template whether it be a HTML or JavaScript file will have some defined text prepended.This will only affective relative links and and will not update absolute links. Examples of the how the middleware will affect the links are below when we add
the prefixexample.comto each link:OriginalWith Middleware/downloadexample.com/downloadexample.com/somethingexample.com/something(no change)/example.com/somethingUse CasesThis is useful for when you need to append a prefix to all links within the templates including static assets.When this is not required, alternative options include adding the prefix directly to the path in your URLs config are viable.This was built with the idea of having multiple Django applications on the same server all be accessible via a single port.Normally, if you want to achieve something similar to this, you would have different applications available on different ports. And so you would have an architecture that looks like the following:Therefore, where the domain is iamsalaah.com, in order to access the main portfolio site, the user would to navigate to iamsalaah.com which is fine as it is served on ports 80/443.But, in order to access Project 1, the user would need to navigate to iamsalaah.com:8001/ and similarly, if they want to access Project 2, they would need to navigate to iamsalaah.com:8002/. Suddenly (at least for me) this starts to look not too great.So, for my own use case, I have Dockerised all the Django projects and have them up and running, each of which are exposing port 80. For the sake of this example, we will imagine there are two projects, and the network names of each of these projects are calledproject_1_networkandproject_2_network. Similarly, the web server services for projects 1 and 2 will be calledproject_1_nginxandproject_2_nginxrespectively.I would then ensure that my portfolio site's web server container have access to bothproject_1_networkandproject_2_networkand would create a something similar to the following configurations (Nginx).server{listen443sslhttp2;listen[::]:443sslhttp2;location/{# Let's imagine that the main portfolio site is running on port 8000.proxy_passhttp://portfolio_site:8000;}location/projects/project-1/site/{rewrite^/projects/project-1/site(.*)/$1break;proxy_passproject_1_nginx;}location/projects/project-2/site/{rewrite^/projects/project-2/site(.*)/$1break;proxy_passproject_2_nginx;}}This configuration would create the following architecture:Therefore, rather then having different ports appearing in the URL, if the user accesshttps://iamsalaah.com/they would be taken to the main portfolio site as normal. But, if they accesshttps://iamsalaah.com/projects/project-1/site, theyproject_1_nginxwebserver will handle the request and show Project 1.
Similarly, if the user were to accesshttps://iamsalaah.com/projects/site/page-3, the webserver (project_2_nginx) would return/page-3from Project 3.The reason including the middleware is to support this behaviour, as we can set theURL_PREFIXERto equal toprojects/project-1/siteandprojects/project-2/sitein the Project 1 and 2 settings respectively.This would mean that, if the user is currently access Project 2's index, and that page has a link topage-1, where the link would normally appear as/page-1, this will be replaced withprojects/project-1/site/page-1. This would be served by the portfolio's webserver and not the webserver belonging Project 1. Therefore, in this case, the user would be navigated toiamsalaah.com/page-1.By including and configuring the middleware, we can now navigate the toiamsalaah.com/projects/project-1/site/page-1which would be served by Project 1's webserver.SetupAdd to Installed AppsAdddjango_url_prefixerto yourINSTALLED_APPSin your settings:INSTALLED_APPS=[# ...,'django_url_prefixer']Enable MiddlewareTo enable to middleware, add to the end of yourMIDDLEWAREin your settings:MIDDLEWARE=[# ...,'django_url_prefixer.middleware.URLPrefixer]Configure URL PrefixerAdd aURL_PREFIXERvariable to your settings with text you want to prefix all
URLs with.URL_PREFIXER='prefix_text'This will in turn update all relative links so that they being withprefix_text.