I would like to run Django from location https://www.example.com/someuri/ so that I have the admin interface at https://www.example.com/someuri/admin/. I can see the login window but when I log in I get redirected to https://www.example.com/admin/.
Where can I set the base URL of Django to https://www.example.com/someuri/? I tried with BASE_URL but with no luck.
Use handy request. build_absolute_uri() method on request, pass it the relative url and it'll give you full one. By default, the absolute URL for request. get_full_path() is returned, but you can pass it a relative URL as the first argument to convert it to an absolute URL.
In Django, views are Python functions which take a URL request as parameter and return an HTTP response or throw an exception like 404. Each view needs to be mapped to a corresponding URL pattern. This is done via a Python module called URLConf(URL configuration) Let the project name be myProject.
To get the full or absolute URL (with domain) in Python Django, we can use the build_absolute_uri method. request.build_absolute_uri(reverse('view_name', args=(obj.pk, ))) to call request. build_absolute_uri with reverse('view_name', args=(obj.pk, ) to get the path of the view with reverse . Then we call “request.
@AgileDeveloper's answer is completely correct for that one off case of admin. However, is the desire to set it this way for all URLs? If so, perhaps you should do something like this
from django.conf.urls import include, url
from . import views
urlpatterns = [
url(r'^someuri/', include([
url(r'^admin/', include(admin.site.urls) ),
url(r'^other/$', views.other)
])),
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With