Django provides useful utilities for working with URLs in your Python code. Two particularly handy methods allow you to extract just the protocol (http vs https) or the host name from a full URL string. In this post, we’ll explore these functions and see examples of how to use urlsplit and get_host to get these URL components.
What You Need Before Starting
Before diving into the code, you’ll want to make sure you have the following:
- A Django project set up
- Access to django.urls for using the urlsplit function
- Access to django.http for the get_host method
With these basics covered, you’ll be ready to parse URLs in your Django app.
Using urlsplit to Get the Protocol and Host
The urlsplit function in django.urls splits a URL string into 5 components:
- The protocol (e.g. http, https)
- The host name (e.g. www.example.com)
- The path
- The query string
- The fragment
To get just the protocol, you can access the 0 index of the returned tuple:
from django.urls import urlsplit
url = 'http://www.example.com/path1/path2?query=true'
protocol = urlsplit(url)[0]
print(protocol) // prints 'http'
The protocol variable will now contain the first component ‘http’. This provides an easy way to check if a URL is http vs https.
Getting the Host Name from a URL
Django’s django.http.request.get_host() function returns the host name portion of a URL string.
For example:
from django.http import request
url = 'http://www.example.com/path1/path2'
host = request.get_host(url)
print(host) // prints 'www.example.com'
The host name can be useful for validating domains, checking CORS permissions, and other tasks related to the request origin.
Use Cases for Getting URL Components
Extracting the protocol and host name from URLs can be helpful in various situations:
- Enforcing HTTPS – Check if protocol is https to require secure requests
- Allowlisting domains – Validate the host against a list of permitted origins
- Analytics – Track visitors from referring URLs
And many more use cases…
Getting URL Parts in View Functions
Here is an example using these URL parsing techniques in a Django view function:
from django.http import HttpResponse, request
from django.urls import urlsplit
def my_view(request):
url = request.build_absolute_uri()
protocol = urlsplit(url)[0]
host = request.get_host()
context = {
'secure': protocol == 'https',
'host': host
}
return HttpResponse(context)
This gets the protocol and host from the request URL to include in the response. The context could then be used in a template, API response, or further logic.
Conclusion
Django provides some handy utilities for getting components of a URL string. The urlsplit and get_host functions allow easy access to just the protocol or host name portions. Making use of these in your views and logic can enable various use cases.
So next time you need to parse URLs in Django, keep these tools in mind! Splitting the full string into parts can save you from messy string manipulation.