Django Template goes to relative url when hyperlinked - html

Hi I have a Django Model like below
class Staff(models.Model):
name = models.CharField(max_length = 200)
url = models.CharField(max_length = 200)
URL's are generated from another website and they are like in the databse
www.foo.com/xxx-yyy
www.foo.com/xxx-zzz
When I use a django template like below, link goes to a related url like an extension of my current URL
{% extends "index.html" %}
{% load markup %}
{% block right %}
<h1>Names</h1>
{% for i in persons %}
<p>{{i.name}}</p>
{% endfor %}
{% endblock %}
It goes to mycurrent.url.com/www.foo.com/xxx-yyy

Ok it is fixed by adding http:// before
like;
{% extends "index.html" %}
{% load markup %}
{% block right %}
<h1>Names</h1>
{% for i in persons %}
<p>{{i.name}}</p>
{% endfor %}
{% endblock %}

Related

Jinja Scope on Inherited Template

I am developing a website using Flask on the back-end and Jinja for templating. Every page should have the same image as part of the Open Graph Protocol, except for one of them, where I want to customize the url with something peculiar to that page.
The child template (like all other templates) extends base, but only this one will ever have a value set for the variable ob_image_url, therefore I need to check if that value exists and if not set a default value.
When the child page.html template is rendered, I am always getting the default value and not the one for that page. Where am I messing it up?
Thanks!
base.html
{% block og_image %}{% endblock %}
{% if not og_image_url %}
{% set og_image_url = url_for('static', filename='img/logo.png', _external=True) %}
{% endif %}
<meta property="og:image" content="{{og_image_url}}" />
page.html
{% extends "base.html" %}
{% block og_image %}
{% if obj and obj.image %}
{% set og_image_url = obj.image %}
{% endif %}
{% endblock %}

Idiomatic way in Jekyll to inherit default data if project specific data unavailable

I am completely new to Jekyll. I did something like this:
{% assign top_nav = site.data.menus %}
{% if site.data.orgs[site.orgData].menus %}
{% assign top_nav = site.data.orgs[site.orgData].menus %}
{% endif %}
<ul>
{% for menu in top_nav %}
<li>
{{ menu.title }}
</li>
{% endfor %}
</ul>
Basically, I will grab an array of navigation items from a default folder. But if I notice the existence of a menu for a specific organization, then I will override the menu provided by the default folder.
What I don't like about this approach is I now have hundreds of places in my jekyll templates that does this if statement check. If this were any other scripting programming language, I would define a function like function($org_name,$prop) {return $site.data.orgs[$org_name][$prop] ? $site.data.orgs[$org_name][$prop] : $site.data[$prop]; } . What would be the idiomatic way to achieve the same objective in jekyll?
I tried a variation of David Jacquel's suggestion by doing this
./org_var.html
{% assign prop = include.prop %}
{% assign orgVar = site.data[prop] %}
{% if site.data.orgs[site.orgData][prop] %}
{% assign orgVar = site.data.orgs[site.orgData][prop] %}
{% endif %}
./_include/nav.html
{% include_relative ../org_var.html prop=menus %}
{% for menu in orgVar %}
... print menu items
./_layout/header.html
{% include_relative ../org_var prop='electronics.televisions' %}
{% for tv in orgVar%}
{{ tv.modelName }}
... print tv values
{% endfor %}
But I get a syntax error in ../org_var.html saying {% include_relative file.ext param='value' param2='value' %} . The documentation says I can't use relative path with include or include_relative. How do I make my org_var.html a reusable and global function? And will electronics.televisions even evaluate properly to the proper path of my site.data.org.[site.orgData][...path] variable?
Just realized there is a default: modifier for a variable like smarty templates.
{% assign top_nav = site.data.orgs[site.orgData].menus | default: site.data.menus %}
You can use Jekyll includes.
From anywhere you want to use your include :
{% include nav.html org_name=org_name prop=prop %}
Will call _include/nav.html that can be something like this :
{% assign org_name = include.org_name %}
{% assign prop = include.prop %}
{% if site.data.orgs[org_name][prop] %}
{% assign top_nav = site.data.orgs[site.orgData].menus %}
{% else %}
{% assign top_nav = site.data.orgs[site.orgData].menus %}
{% endif %}
<ul>
{% for menu in top_nav %}
...

Django, display certain hyperlinks based on user group

{% extends 'base.html' %}
{% block content %}
<p>Welcome to home page.</p>
<p>{% user.groups.all() %}</p>
{% endblock %}
At the moment I'm trying to figure out how I could even get all the user's groups to show on the page. This results in an error.... Invalid block tag on line 5: 'user.groups.all()', expected 'endblock'. Did you forget to register or load this tag?
I have tried to do if statements, but it seems break as soon as it meets one condition. For example if user is a part of test1 and test2 groups, I'd like for it to display test1 and test2, but it only displays test1.
{% extends 'base.html' %}
{% block content %}
<p>Welcome to home page.</p>
{% if user.groups.all.0.name == "test1" %}
<p>test1</p>
{% if user.groups.all.0.name == "test2" %}
<p>test2</p>
{% endif %}
{% endblock %}
In the first code, you should have used {{ }} instead. In order to access groups, do this:
{{ user.groups.all }}
and to check for a specific group:
{% if desired_group in user.groups.all %}
some html..
{% endif %}
if you want to output specific html for each group:
{% for group in user.groups.all %}
{% if group == desired_group %}
some html..
{% endif %}
..some more conditions..
{% endfor %}

how to displayliquid code in Jeckyll post

Hello I am trying to make a post using jeckyll and as part of my post I would like to show some liquid code. The post should display the IF statement as part of the post text (example below).
{% if customer and customer.tags contains 'Wholesale' %}
{% endif %}
I have tried to post this as
{% highlight liquid %}
{% if customer and customer.tags contains 'Wholesale' %}
{% endif %}
{% endhighlight %}
and also
{% highlight markdown %}
{% if customer and customer.tags contains 'Wholesale' %}
{% endif %}
{% endhighlight %}
but anything I try seems to be still executing the liquid code.
Is there a way to display the IF statement my post?
Try wrapping your liquid code in {% raw %} {% endraw %} like this:
{% highlight liquid %}
{% raw %}
{% if customer and customer.tags contains 'Wholesale' %}
{% endif %}
{% endraw %}
{% endhighlight %}
The raw tag will disable any liquid processing and output your code as desired.

Cannot load multiple commenting system in Jekyll blog

I am trying to implement disqus and facebook commenting system for different categories in jekyll blog.
Here is my current approach.
{% for category in site.categories %}
{% if category.type == "personal" %}
{% include facebook.html %}
{% else %}
{% include disqus.html %}
{% endif %}
{% endfor %}
Expected result: Facebook comment should be loaded in category personal from facebook.html otherwise disqus comment should be loaded in all other category.
Actual result: Disqus comments loads automatically in personal category regardless of the loop.
What should be changed to make the comments load correctly?
It seems that you want to print disqus or facebook on each page depending on "personnal" category presence.
As #marcanuy says, you have to refer to page.categories, which is an array.
{% if page.categories contains "personnal" %}
{% include facebook.html %}
{% else %}
{% include disqus.html %}
{% endif %}
Category doesn't have a type attribute. Checking directly like:
{% for category in page.categories %}
{% if category == "personal" %}
{% include facebook.html %}
{% else %}
{% include disqus.html %}
{% endif %}
{% endfor %}
Should detect the personal category and load disqus comments.