Attaches a prefix to all links in a Django web application.
Project description
Django URL Prefixer
A 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 prefix example.com
to each link:
Original | With Middleware |
---|---|
/download |
example.com/download |
example.com/something |
example.com/something (no change) |
/ |
example.com/something |
Use Cases
This 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 called project_1_network
and project_2_network
. Similarly, the web server services for projects 1 and 2 will be called project_1_nginx
and project_2_nginx
respectively.
I would then ensure that my portfolio site's web server container have access to both project_1_network
and project_2_network
and would create a something similar to the following configurations (Nginx).
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
location / {
# Let's imagine that the main portfolio site is running on port 8000.
proxy_pass http://portfolio_site:8000;
}
location /projects/project-1/site/ {
rewrite ^/projects/project-1/site(.*) /$1 break;
proxy_pass project_1_nginx;
}
location /projects/project-2/site/ {
rewrite ^/projects/project-2/site(.*) /$1 break;
proxy_pass project_2_nginx;
}
}
This configuration would create the following architecture:
Therefore, rather then having different ports appearing in the URL, if the user access https://iamsalaah.com/
they would be taken to the main portfolio site as normal. But, if they access https://iamsalaah.com/projects/project-1/site
, they project_1_nginx
webserver will handle the request and show Project 1.
Similarly, if the user were to access https://iamsalaah.com/projects/site/page-3
, the webserver (project_2_nginx
) would return /page-3
from Project 3.
The reason including the middleware is to support this behaviour, as we can set the URL_PREFIXER
to equal to projects/project-1/site
and projects/project-2/site
in 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 to page-1
, where the link would normally appear as /page-1
, this will be replaced with projects/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 to iamsalaah.com/page-1
.
By including and configuring the middleware, we can now navigate the to iamsalaah.com/projects/project-1/site/page-1
which would be served by Project 1's webserver.
Setup
Add to Installed Apps
Add django_url_prefixer
to your INSTALLED_APPS
in your settings:
INSTALLED_APPS = [
# ...,
'django_url_prefixer'
]
Enable Middleware
To enable to middleware, add to the end of your MIDDLEWARE
in your settings:
MIDDLEWARE = [
# ...,
'django_url_prefixer.middleware.URLPrefixer
]
Configure URL Prefixer
Add a URL_PREFIXER
variable 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 with prefix_text
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-url-prefixer-0.1.0.tar.gz
.
File metadata
- Download URL: django-url-prefixer-0.1.0.tar.gz
- Upload date:
- Size: 48.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f64ff3bd62e2bfb28f7d1f6d39320c9633e20a4e1625f058f3d171e9475f4b29 |
|
MD5 | f03f761f91f9789fdfb28908fbb79653 |
|
BLAKE2b-256 | 6cc2bdda386e9d89de71c707a518d9c5e7d1047e7449c8e103709197120fb9d1 |
File details
Details for the file django_url_prefixer-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: django_url_prefixer-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a26213195c5cdc4cc5ad8526a4ce79b8ebb88f0d069714b05b434802a31f9c97 |
|
MD5 | 482ef00e78ef72ca89c6840f574007da |
|
BLAKE2b-256 | 21a216ba2796aef00f5919f9e908cd2a4cb5272e5794fd1964056dd2ed1afe2a |