So there should be a very basic way to do this, but unfortunately I don't seem to be able to find it.
How can one set an Href link to point to the 'base website url' + the 'link', rather than adding the link to the current page.
I.e. if I'm at www.example.com/content1/
I want the search function to go to www.example.com/search/
and not www.example.com/content1/search
I could just specify "www.example.com/search/" but then if it page is deployed locally I end up with a bunch of links to non-existent pages or vice versa. How can I specify the The Base hosting URL using DJango (whichever the server is running, whether the hostname, the current server ip, localhost etc.).
The URL found in the address bar of the front page of a website is its base URL. In other words, the common prefix found while navigating inside a given website is known as the base URL. One can select a base URL from the list of those available with help of the URL general properties page.
Set up app folder's urls.py and html files In the same directory, you should have a file named views.py. We will create a function called index which is what makes the http request for our website to be loaded. Now, we've set it up such that http://127.0.0.1:8000/homepage will render the HTML template index.
To specify a default URL location, use the SET BASEURL command. Using SET BASEURL puts <BASE HREF="url"> into the HTML file that WebFOCUS generates. When a report is run, the specified directory is searched for the HTML files, graphics files, and Java applet CLASS files that are called by the generated webpage.
The most basic technique to name Django urls is to add the name attribute to url definitions in urls.py . Listing 2-16 shows how to name a project's home page, as well as how to reference this url from a view method or template.
The best way to do this is the name your urls and then use the url template tag. Example below:
First, name your views. Use something like:
urlpatterns = [
...
url(r'^search/$', views.search_view, name="search"),
...
]
In this example, you've got your url for your example.com/search/
view. It is named 'search', which can be used url
template tags and using the reverse()
function.
Next, in your template, use the url
tag with your url name:
<a href="{% url 'search' %}">Search</a>
You shouldn't need to add 'base website url' to your href, it is implied. Make sure href is prefixed with '/' to set and absolute path and no '/' for relative.
<a href="/">home</a>
is the same as
<a href="http://www.mywebsite.com/">home</a>
and will work no matter which sub directory you are in
If you are on the homepage and you use the link:
<a href="sample">sample</a>
it will effectively equal:
<a href="http://www.mywebsite.com/sample">sample</a>
but that same link used on the page http://www.mywebsite.com/sample will equate to:
<a href="http://www.mywebsite.com/sample/sample">sample</a>
using:
<a href="/sample">sample</a>
Will always equate to the following no matter where on the site it is used:
<a href="http://www.mywebsite.com/sample">sample</a>
If you are using django consider using the url template tag as Alex suggested:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#url
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