Fetch blog entries with bootstrap custom theme and mezzanine - django

I'm new to mezzanine, I managed to install my custom bootstrap theme just copying templates and static files in the relatives folders in my django app.
Assume in my index.html I have some blog entries like these
<h2>Other Entries</h2>
<article>
<h3>Blog Post 1</h3>
<p>Contrary to popular belief, Lorem Ipsum is not simply random text.... Read more</p>
</article>
<article>
<h3>Blog Post 2</h3>
<p>Contrary to popular belief, Lorem Ipsum is not simply random text.... Read more</p>
</article>
<article>
<h3>Blog Post 3</h3>
<p>Contrary to popular belief, Lorem Ipsum is not simply random text.... Read more</p>
</article>
How can I fetch my blog entries I've previously inserted in the admin page?
Thanks

At the top of your template put:
{% load blog_tags %}
Then wherever you want the blog posts to show up put something like the following
{% blog_recent_posts as recent_posts %}
{% for blog_post in recent_posts %}
<h3>{{ blog_post.title }}</h3>
{{ blog_post.description_from_content|truncatewords_html:10|safe }}
Read more
</article>
{% endfor %}
Shameless plug:
I'm in the middle of writing a series of blog posts that describes the process of how I create themes for Mezzanine. Check it out, http://bitofpixels.com/blog/mezzatheming-creating-mezzanine-themes-part-1-basehtml/

Related

How to store nav menu urls in database?

I'm working on my first django app, and i have side nav menu like in twitter. To prevent the dozens of lines in my template like this
<ul class="nav d-flex flex-column">
<li class="nav-item align-self-start rounded-pill mb-3"><li>
<li class="nav-item align-self-start rounded-pill mb-3"><li>
...
<li class="nav-item align-self-start rounded-pill mb-3"><li>
</ul>
and for app extensibility i want to store nav menu in database to be able to loop over the menu items
<ul class="nav d-flex flex-column">
{% for item in menu %}
<li class="nav-item align-self-start rounded-pill mb-3"><li>
{% endfor %}
</ul>
But the problem is that i can't store direct urls for menu items in database, because several of them have dynamic urls e.g. profile page, which has 'slug:username/' pattern.
I've tried to store template tags in database like
{% url 'app_name:view_name' %}
but of course it doesn't work.
My current idea is to store in database namespaced url e.g. 'app_name:view_name' for static urls and 'request.user.get_absolute_url()' for pages which have username in urls.
The next step is to get QuerySet with menu items from database, loop over them and transform namespaces url with reverse (it works), but 'request.user.get_absolute_url()' is just a string and it doesn't work. Then make list of ditcs and pass it to context
menu = [{item1 attrs}, {item2 attrs},...,]
Is exists a better approach to solve my problem? And finally what i should do with dynamic urls?
UPD:
If we're dealing with menu items whose urls depend only on usernames i.e. only on User model, we can do smth like this (NavigationMenu - table model, url_link - column with following values: 'app_name:view_name' or 'get_absolute_url'):
menu = NavigationMenu.object.all()
for item in menu:
url_link = item.url_link
if ':' in item.url_link:
url = reverse(url_link)
else:
method = getattr(request.user, url_link)
url = method()
# then store all instance attrs in dict
But it seems that is not a good way to solve this.
The ideal approach to achieve this is to create a base.html in which you build this sidebar nav item, and then include that base.html on every page where you want that sidebar.
This is an example of base.html
<!DOCTYPE html>
<html>
<head>
<title>My Project</title>
</head>
<body>
<header>
<ul>
<li>Home</li>
<li>News</li>
</ul>
</header>
{% block content %}{% endblock content %}
</body>
</html>
Include base.html in other Html files
{% extends "base.html" %}
{% block content %}
<h2>Content for My App</h2>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry..</p>
{% endblock content %}

How can I capitalize first word of "lorem" template tag output in Django 2.2?

Django provides a useful "lorem" template tag that will output some "lorem ipsum" text, but the text is all lowercase. How can the first word be capitalized, to get "Lorem ipsum ... "? I know that if I had a string I could run it through a filter, but I don't know how to filter the output of a tag.
UPDATE:
I thought I had found the answer:
<p>
{% filter capfirst %}
{% lorem 10 w random %}
{% endfilter %}
</p>
But it doesn't work (even though I restart the dev server). If I change the filter to "title", that works, making every word start with a capital letter ("Lorem Ipsum Set Delor..."). Why doesn't capfirst work in the filter tag?
Never mind, have found the solution:
{% filter capfirst %}{% lorem 10 w random %}{% endfilter %}
There must be no whitespace separating the filter tag and the lorem tag.
{{ value|capfirst }}
If value is "django", the output will be "Django".

reversed counting in django template with <ol>-tag

I want to write a template which
is paginated (not all list items on the same page)
is numbered with -tag because this is the most semantic way to order the list
can be reversed or not
the numbers are linked to the represented models (every li-tag contains an a-tag that is linked to a model.) That means the list not alway starts with 1.
My model:
class Mymodel(models.Model):
number = positiveIntegerField()
title = models.CharField(max_length=100)
[...]
number is unique, ordered and when you order the models with the number, there is no gap; number is 1-based
What I have: (reverse is boolean variable which tells whether the list has to be reversed or not)
{% if reverse %}
<ol class="content" start="{{ article_list.0.number|add:article_list.count }}" reversed=true>
{% for article in article_list reversed %}
<li class="{% cycle '' '' '' '' 'seperate-bot border-gray' %}">{{ article.title }}</li>
{% endfor %}
{% else %}
<ol class="content" start="{{ article_list.0.number }}">
{% for article in article_list %}
<li class="{% cycle '' '' '' '' 'seperate-bot border-gray' %}">{{ article.alt_title }}</li>
{% endfor %}
{% endif %}
</ol>
Unfortunately counts the backward template part wrong: the last number is always 1 which it shouldn't be. The last number should be article_list.0.number + article_list.count (or: article_list|length) -1; or last_list_item.number that would be better.
That means: (paginated_by = 3 for that example) the "|" because SO changes my counting
How it should be: (reverse=true)
page 1:
|3. lorem ipsum
|2. lorem ipsum
|1. lorem ipsum
page 2:
|5. lorem ipsum
|4. lorem ipsum
What I get: (only page 2 is bad)
page 2:
|2. lorem ipsum
|1. lorem ipsum
Is there a good way to avoid that effect? (And where does it come from?) In the best case that should work in the template only.
I'm not sure why you would get that result, but this seems like a overly complicated way to do something quite straightforward.
You can use the built in last filter and with template tag to access the last item in a list.
{% with article_list|last as last_article %}
<ol class="content" start="{{ last_article.number }}" reversed=true>
{% endwith %}
However, I would recommend you do the ordering and reversing in the view function instead of in the template. Django's template language is by choice not suited for non trivial logic.
As for the numbering, you can actually explicitly assign a value attribute to a ordered list element. This would solve your problem.
<li value={{ article.number }} ...
Example:
<ol>
<li value=3>it doesn't
<li>have to
<li>make
<li value=42>sense
</ol>

Django and tabbed navigationin templates

I am building a django project which among others has a customer model. Any customer can be a foreign key to some calendar entry models or some picture models. I want to be able to get in to a customers page (e.g domain/customoer/1) and be able to navigate to the different models the customer is related with (e.g all the pictures for the customer, all the calendar entries of the customer). I am using bootstrap for the "presentation of the site. My idea was to use tabs. So I created a pil for my main template the customer.html
<ul class="nav nav-pills">
<li class="active">Personal Data</li>
<li>History</li>
<li>Analysis</li>
<li>Diagnosis</li>
<li>Treatment</li>
<li>Appointments</li>
<li>Graphics</li>
</ul>
and i was thinking of including a template for each pill
<div class="tab-content">
<div id="personal-data" class="tab-pane active ">
<div id="history" class="tab-pane">History is in the making</div>
<div id="analysis" class="tab-pane">
{% include 'customer/analysis.html' with customer=customer %}
</div>
<div id="diagnosis" class="tab-pane">Diagnosis is differential</div>
<div id="treatment" class="tab-pane">Treatment what treatment??</div>
<div id="appointments" class="tab-pane">Ap point ment</div>
<div id="graphics" class="tab-pane">Now this is going to be hard!</div>
The templates in each tab can do different things like upload pics navigate to different pages etc.
When i hit on a pill the url (domain/customer/1/) won't change (naturally) to inform me to which tab i am at the moment. So my question is how can i know in which tab i am ath the moment? I want the user to be able to do different things from different tabs (upload pics etc) and I want to be able to redirect to the specific tab after the view is called? Maybe #id could be appended to the url to specify the tab, but it doesn't happen with bootstrap.
What is the best way to deal with tabs in django? Ajax maybe? Will I not have the same problem? Any ideas or lings would be nice too
I use template inheritance for this kind of thing. It's simple and flexible. For example, you can define your main navigation in your base template like so:
...
<li {% block news %}{% endblock %}>News</li>
<li {% block features %}{% endblock %}>Features</li>
<li {% block sport %}{% endblock %}>Sport</li>
...
Then, in your base templates for each of those apps you'd have something like:
<!-- news/base_news.html -->
{% extends 'base.html' %}
...
{% block news %}class="active"{% endblock %}
...

How to state correctly the author informations of an article with microdata?

I'm using the microdata system for some articles.
But I can't find some good ressources to know if the structure of my schema, for the author, is good or not.
"Article"'s shema allow to specify an "author", but how to indicate the informations (image, link, name) about this author ?
Actualy I use the following (fiddle):
<div itemscope itemtype="http://schema.org/Article">
<h1 itemprop="name">Article title</h1>
<div itemprop="articleBody">
Lorem ipsum dolor sit amet...
</div>
<div itemprop="author" itemtype="http://schema.org/Person" class="author">
<a href="member-page.html" itemprop="url">
<img itemprop="image" src="http://pic.okisurf.com/member/none/xs/m.png"/>
<span itemprop="name">Author name</span>
</a>
</div>
</div>
Does this line is correct ?
<div itemprop="author" itemtype="http://schema.org/Person">
If it is not, any help or ressource link will be greatly appreciated.
It seems that to specify a new itemtype inside another, we need to specify another time the itemscope !
So this line will be correct :
<div itemscope="" itemprop="author" itemtype="http://schema.org/Person">
To make sure that the microdata are well formed, I suggest to use the Webmaster Tools richsnippets test