django template href external file - django

I use this template to list links:
<a class="col-sm-offset-1 col-sm" href="{{ oLink.link }}" target="_blank">{{ oLink.text }}</a>
oLink.link could be something like: 'http://www.google.com' or '\\shared_files\GMO-M0960\GMO-M0960_01.pdf'
oLink.text is just a simple text to be used.
Links reference to another webpage work correctly, but links reference to files inside a shared directory don't (new window never opens)
It works only if I right click on the link and then copy its url in a new window.
Do I need to configure something? This seems to be so simple.

Related

Cannot link to static files with Hugo

I have this in config.toml:
baseURL = "https://my-username.github.io/blog/"
and there is a static file at static/img/foo.png.
Now, in content/posts/bar.md, I have the following content:
---
title: "Bar"
---
![foo](img/foo.png)
The picture isn't showing after I started the hugo server, so I inspected elements, and found out that Hugo generated the following URL for it:
http://localhost:1313/blog/posts/bar/img/hireme.png
This is not what I expect; it should be
http://localhost:1313/blog/img/hireme.png
When I use ![foo](/blog/img/foo.png), the picture is displayed correctly, but this is quite strange: /blog/ is part of baseURL, why do I need to type it again?
I think you should use <base> tag to make baseURL for static files.
Add the <base> tag into <head>:
<base href="{{ .Site.BaseURL }}">
And then you can insert image in post like this:
![Foo image](img/foo.jpg)
References:
https://www.w3schools.com/tags/tag_base.asp
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
In a simple setup where your site lives in the domain's root, a absolute reference to the image would be a standard practice. However, in your case where the site is nested, a more crafty solution is necessary.
I suggest using a shortcode to solve your dilemma. Just simply create a shortcode that takes in a resource file path and spits out the desired absolute path every time. The advantage to this approach is flexibility for future resource relocation to a cdn for example.
Your markdown code will be something like this:
![foo]({{< resource url="img/foo.png" >}})
Your shortcode template will be something like this:
{{ .Site.BaseURL }}{{ .Get "url" }}
Create a file named 'resource.html' in 'layouts/shortcodes/' folder, and drop in the shortcode template code above.
So when you decide you want to switch to a cdn you could easily do this:
://cdn.example.com/{{ .Get "url" }}

Link to a specific div in another app(page) in Django using anchor tag

I have a div with a certain id
<div id="services">
Then I try to link to it using Django templates
<a href="{% url 'homepage' %}#services">
But it only works if I'm in the same page (App)
Is there a way to work around this ?
I found out what the problem was. I had a script that does smooth scrolling and it had "event.preventDefault();" in it. as I removed that it worked.

Django: correct URL appears in developer tools but link doesn't use that src

I've created a link in a Django template which appears to lead to the correct URL in the elements pane of the developer tools. But the actual link in the page only goes to localhost. I thought I understood how the Django url template tag worked, but I must be missing something.
URL pattern:
url(r'^dollhouse/(?P<dollhouse>[0-9]+)$', views.dollhouse, name='dollhouse')
Template:
{% for workingdollhouse in dollhouses %}
<a href src="{% url 'dollhouse' dollhouse=workingdollhouse.id %}">{{workingdollhouse.dollhouse_name}}</a>
{% endfor %}
Text of element appearing in developer console (hovering over the '"/dollhouse/1"' reveals the intended address of localhost/dollhouse/1):
'<a href src="/dollhouse/1">dollhouse1</a>'
Actual link just goes to localhost.
This has nothing to do with Django.
a elements don't have a src attribute. The destination goes in the href attribute, which you left blank.
{{workingdollhouse.dollhouse_name}}

display static html asset in jekyll + github pages

I want to add a link to a static html page (which I have created, not hosted anywhere). I added the html file to my images folder (contains the images which are being rendered in my jekyll blog). Then I make a hyperlink to the html files which I just added in the images folder, like this:
<a href=https://raw.githubusercontent.com/USERNAME/USERNAME.github.io/master/images/time.vs.score.html>View plots here</a>
Where username is my github username. This does not open a new html page, instead I just see the plain text of the html file. How can I add static html files are part of the assets? Thanks!
Thats what raw.githubusercontent.com is made returning raw file.Files are returned with Content-Type:text/plain; and then displayed as text by your browser.
You'd better link to github pages published content at https://USERNAME.github.io/images/time.vs.score.html
I found an easy solution using this tool: https://rawgit.com/
just replace:
<a href=https://raw.githubusercontent.com/USERNAME/USERNAME.github.io/master/images/time.vs.score.html>View plots here</a>
with:
<a href=https://cdn.rawgit.com/USERNAME/USERNAME.github.io/master/images/time.vs.score.html>View plots here</a>

Django path to current page

I have a django template page, and want a link from this page, containing current URL, for example, I am on /article/11 and want link to /article/11/remove
I tried the following construction:
Remove article
But I get link to /article/remove instead of /article/11/remove
However when I change it to
<a href="{{ request.path }}">
I get link to /article/11
How can I get URL not trimmed?
I don't see why it doesn't point you to /article/11remove, which is what it sounds like it should do, but either way, you're missing a slash. Try <a href="{{ request.path }}/remove"> instead.
However, that's really not the right way to do it. You shouldbe using {% url 'name_of_remove_view' %} to get the url, not assuming it's going to be wherever you are plus /remove.
Edit: In that case, your problem is probably that {{ request.path }} is not outputting anything at all. That would explain why just having "remove" would take you to /article/remove, and having "" would take you to where you currently are, due to the way that relative URLs work. You might want to make sure that you have a request object at all in your template environment.