Django get current domain in template




















The following output will appear after executing the script. Here, the path is index2. The geturl3 function will be called for this URL that will send the domain name with the http and the path to the index3. Here, the path is index3. The current URL can be displayed in the Django template using the method and the attributes of the request object. Three templates had been created in this tutorial to display the current URL in three different ways that will help the readers to know the way of reading the current URL in the Django template.

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. Django template is used to display the application data for the users. Sometimes it retrieving and displaying of the current URL in the template.

The current URL can be displayed in different ways, such as only the domain name, a domain name with path, and the domain name with path and http. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.

The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually.

Linked 0. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled. Accept all cookies Customize settings. I like the following library when reverse referencing absolute urls:. As mentioned in other answers, request. However, my use case was slightly different. To resolve this, you have to use some kind of environment variable.

You can either use 1 an environment variable something like os. Instead of installing sites framework just for this or doing some of the other stuff mentioned here that relies on request object, I think the better solution is to place this in models. It could be as simple as:. I came across this thread because I was looking to build an absolute URI for a success page. Here for example am saying load the form and tell the form that the next URL is the current URL which this code rendred from.

Not for absolute url but I was looking just to get host. If you want to get host in your view. I'm not entirely sure how it works. I believe this is a bit more low level and will return your server hostname, which might be different than the hostname used by your user to get to your page. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Ask Question. Asked 11 years, 10 months ago. Active 2 months ago. Viewed k times. I want to use it with reverse.

See: code. Add a comment. Active Oldest Votes. Flimm k 35 35 gold badges silver badges bronze badges. Dmitry Shevchenko Dmitry Shevchenko How can I see the part after sharp?

And what if I don't have access to request? Show 9 more comments. Thanks for the helpful answer. Nothing better than code itself. Helped me! Thanks for the help buddy, really appreciated it. This is really handy when you don't have access to HttpRequest object.

To change example. Just do Site. Now Site. My development domain is at " I want my Django to find own its own which domain is it running on. Read more in documentation for using the sites framework e. Darb Darb 1, 10 10 silver badges 9 9 bronze badges. I believe my question specifically said "without the Sites module". Does this hit the DB? The Sites module has been written to cache Site objects using module level caching i.



0コメント

  • 1000 / 1000