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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-07-08 17:20:45 +0000
Seen: 15 times
Last updated: Jul 08 '23
What is Fullscreen Activity in Android?
What does 'Invalid argument (callbackUrlScheme): must be a valid URL scheme' mean?
How can SSL passthrough be implemented with Traefik in Kubernetes?
What are the steps to create a semi-circular shape divided into 8 parts using HTML, CSS, or SVG?
What is the way to name parameters and REST API urls in Spring Boot?
How can ASP.NET Core be configured to incorporate various authorization strategies?
What are the steps to adjust the dot size in a plot created with mpl-scatter-density?