Call links list programatically for Pelican site - python

I have a Pelican blog. I want to call the external links list programmatically, rather than hard code them into the template. For example, the blog post categories are called programmatically, e.g.,
{% for category, articles in categories[0:10] %}
<li>{{ category }}</li>
{% endfor %}
</div>
<div class="l-box pure-u-1-3">
{% for category, articles in categories[11:20] %}
<li>{{ category }}</li>
{% endfor %}
</div>
<div class="l-box pure-u-1-3">
{% for category, articles in categories[21:30] %}
<li>{{ category }}</li>
{% endfor %}
So to be clear, I am looking to change this code to call from a single file which lists some external weblinks.

Assign them to the LINKS variable in your pelicanconf.py e.g.
LINKS = (
('my link 1', 'http://some.link.here'),
('my link 2', 'http://some.other.link.here')
)
and then call them in your template with
{% for name, link in LINKS %}
{{ name }}
{% endfor %}
All variable defined in your pelicanconf.py, as long as they are in all-caps, can be accessed in your templates.
See: http://docs.getpelican.com/en/3.5.0/themes.html#templates-and-variables

Related

how to use a django variable in an <a> tag's href?

I want to create a list of entries in which each entry is linked to its page like this: wiki/entry-title. I'm using a for loop to add <li>s to HTML. here's the code:
<ul>
{% for entry in entries %}
<li>{{ entry }}</li>
{% endfor %}
</ul>
urlpattern:
path('wiki/<str:title>', views.entry, name='entry')
what should I type in href to link the <li> to wiki/entry?
You can set your value in url as
<ul>
{% for entry in entries %}
<li>{{ entry }}</li>
{% endfor %}
</ul>
Its better to use {% url %} [Django-doc] template tags as
<ul>
{% for entry in entries %}
<li>{{ entry }}</li>
{% endfor %}
</ul>
NOTE : change value with your value accordingly. For e.g. {{entry.value}} or {{entry.title}} or {{entry.id}}
There is a clear example in the django docs
<ul>
{% for yearvar in year_list %}
<li>{{ yearvar }} Archive</li>
{% endfor %}
</ul>
In django, the template parser always handles django code first, then html/javascript. So you would insert a django variable into an anchor tag the same way you'd insert it anywhere in the template and the parser will replace it before it tries to render the html. If it's a django url, you can use the {% %} format as referenced in the previous answer, and if it's a url that's perhaps stored on the object, you can just use {{ }} (like {{ entry.wiki_url }}). You can also use text for some of the url and a variable for part. So if you have a wiki site that has a base url of, for instance, https://mywiki.com you'd write the href like:
<ul>
{% for entry in entries %}
<li>{{ entry.title }}</li>
{% endfor %}
</ul>
I found out that the only solution that was working for me was this format
{{ x }}
x: this is variable that we want to implement inside href attribute
'url_path_name' : this is the name of the url we gave in urls.py , see :
path("wiki/<str:name>" , views.entry, name="url_path_name")
So this defined url should look like this:
wiki/ ?
href="{% url 'url_path_name' x %}"

Flask Jinja Route

What is the correct syntax for the href on an html page with Jinja2 code, that allows for navigation between two pages? The first html template has a list of names, while the other has person details.
Here is the code I have for the listOfNames.html page that displays the list of names.
<ul>
{% for rownum, row in listNames.iterrows() %}
<li>{{ row.firstName }} {{ row.lastName }}</li>
{% endfor %}
</ul>
Here is the server.py code that gets/puts (correct use of term?) the names on the listOfNames.html.
#app.route('/listNames/<bo>/')
def listNames(bo):
listNames = getListNames(bo)
return render_template('listOfNames.html', listNames=listNames)
This is code for the personInformation.html.
<main role="main" class="col-sm-9 ml-sm-auto col-md-10 pt-3">
<h1>{{ person.firstName }}
{{ person.lastName }}
</h1>
<h2>Office:
{{ person.bo }}
</h2>
<h2>Courses Completed
</h2>
<ul>
{% for rownum, row in personCompleted.iterrows() %}
<li><a href="/courses/{{row.courseId}}">
{{row.courseTitle}}
</a></li>
{% endfor %}
</ul>
</main>
And here is the server.py code.
#app.route('/people/<person_id>')
def person(person_id):
person = getPersonOrganization(person_id)
personCompleted = getPersonCompleted(person_id)
return render_template('personInformation.html', person=person, personCompleted=personCompleted)
The best way to do this is by using the url_for() function from Flask. Here is your new listOfPeople.html template with the link:
<ul>
{% for rownum, row in listNames.iterrows() %}
<li>{{ row.firstName }} {{ row.lastName }}</li>
{% endfor %}
</ul>
It's best to not hardcode your URLs in the templates, because if you ever need to reorganize them, then you would need to update URLs all over the place. With url_for() Flask takes care of generating the URLs for you using the information you provided in the app.route decorators.

Querying for specific articles (via tag/category) in Pelican themes

Is it possible to set query parameters via jinja in Pelican template files?
index.html
{% if articles %}
{% for article in articles_page.object_list if article.category == 'article' %}
#stuff
{% endfor %}
{% endif %}
This will return articles in the article category, but only if they happen to be in the articles already queried for. The desirable setup would be to grab x articles in y category (or with y tag) - is that possible?
This code snippet works for me to bring back a list of all articles matching a tag:
{% block content %}
<ul>
{% for article in articles if FAVORITES_TAG in article.tags %}
{% if loop.index <= FAVORITES_COUNT %}
<li>{{ article.title }}</li>
{% endif %}
{% endfor %}
</ul>
{% endblock %}
I set the FAVORITES_TAG and FAVORITES_COUNT variables in the config. I hope that helps.
I ran into the same problem, and found a solution
{% if articles %}
{% for article in articles_page.object_list if article.category.name == 'article' %}
#stuff
{% endfor %}
{% endif %}

How To Access Many to Many Attribute in Django

I am very new to web development and I have created a sample project using Django. So far I have a Django powered page that displays the contents of one of my database's model objects which is called Publications. The code I have in my view template is:
<html><head><title>Publications</title></head>
<body>
<h1>Publications</h1>
<ul>
{% for publication in publication_list %}
<li>{{ publication.title }} </li>
{% endfor %}
</ul>
</body></html>
This works fine, but now I would like to access and display a many to many attribute on Publications called Tags. I have tried adding another for tag as follows:
<html><head><title>Publications</title></head>
<body>
<h1>Publications</h1>
<ul>
{% for publication in publication_list %}
<li>{{ publication.title }} </li>
{% for tag in publication_list.tags %}
<li>{{ tag.title }} </li>
{% endfor %}
{% endfor %}
</ul>
</body></html>
I realize this is quite wrong, but I don't see how to access the Tags model. For reference, my function for displaying the publications in the view is:
def display_publications(request):
publication_list = Publication.objects.order_by('title')[:10]
return render(request, 'publications.html', {'publication_list': publication_list})
And my Publications and Tag Models are:
class Tag(models.Model):
title = models.CharField(max_length=50)
class Publication(models.Model):
title = models.CharField(max_length=200)
tags = models.ManyToManyField(Tag, blank=True)
Any help is appreciated.
What you are doing only accesses the ManyRelatedManager. You need to specify a query against that manager. In python, it would be:
publication.tags.all()
In a django template it would be:
{% for tag in publication.tags.all %}
{{ tag }}
{% endfor %}
This should be covered in the official documention on many-to-many relationships.
Edit: Here's a good example of how many-to-many relationships work: https://docs.djangoproject.com/en/1.5/topics/db/examples/many_to_many/
Because you seem to be having some trouble with this, given your comments on the other question, here are the changes to the template. You do not need to modify the view at all from what you have given above.
{% for publication in publication_list %}
<li>{{ publication.title }}
<ul>
{% for tag in publication.tags.all %}
<li>{{ tag.title }} </li>
{% endfor %}
</li>
</ul>
{% endfor %}

Page not found after click in Django

I have a simple menu-list
{% block menu %}
<ul>
{% for item in list %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% endblock %}
and a list:
MENU = ['Home','Contact','About']
When I press Contact then go to the address 127.0.0.1:8000/Contact
with this same template but if I again click Contact I'm getting Page not found at 127.0.0.1:8000/Contact/Contact
What can I do about this?
The particular issue here is that the HTML you emit has links to "Contact/", which if you're already at a /Contact URL, will go to a /Contact/Contact like you see here.
The quick fix is to add a / right before the {{item}}, like this:
<li>{{ item }}</li>
However, django has better ways to handle URLs than creating them yourself. Look at the URL dispatcher documentation [1], with the intention of being able to use the url template tag [2].
What that line would end up looking like then is something like this:
<li>{{ item }}</li>
[1] http://docs.djangoproject.com/en/dev/topics/http/urls/
[2] http://docs.djangoproject.com/en/1.2/ref/templates/builtins/#url
Try changing href="{{item}}" to href="/{{item}}":
(% block menu %}
<ul>
{% for item in list %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% endblock %}
You were using a relative URL which won't work if you're in a different level of the URL path.
Use absolute paths, for example:
MENU = [{'text':'Home',
'url':'/home'},
{'text':'Contact',
'url':'/contact'},
{'text':'Home',
'url':'/home'}]
And code like this:
{% block menu %}
<ul>
{% for item in list %}
<li>{{ text }}</li>
{% endfor %}
</ul>
{% endblock %}
But better solution would be to use some ready CMS app, for example from here django resources page like django-cms ( django-cms.org ).

Categories

Resources