1 | initial version |
In a Django app, the current base URL path can be obtained using the request
object. Here's an example:
from django.shortcuts import render
def my_view(request):
base_url = request.scheme + '://' + request.get_host() + request.path
return render(request, 'my_template.html', {'base_url': base_url})
In the above code:
request.scheme
returns the protocol (http or https)request.get_host()
returns the hostname of the server (e.g. www.example.com)request.path
returns the path of the current URLThese three values are combined to create the base_url
. This value can then be used in the view or passed to the template as a context variable.