django-tables2 to used with django-modeltranslation showing all the fields when listed - django-tables2

I am using django-tables2 with django-modeltranslation in one of my new projects. The site is using localization for english and french.
If I render the table directly in html everything works fine, but django_tables2 seems to pour everything out, ignoring the localization request.
Django-tables2 has a "localize/unlocalize" option for multilingual sites but it does not seem to work with django-modeltranslation.
Here is my setup.
models.py
class DataSet(models.Model):
name = models.CharField(max_length=255)
source = models.CharField(max_length=255, null=True)
data_type = models.CharField(max_length=255, null=True)
translation.py
#register(DataSet)
class DataSetTranslationOptions(TranslationOptions):
fields = (
"name",
"source",
"data_type",)
tables.py
class DataSetTable(tables.Table):
name = tables.Column(order_by="name")
class Meta:
model = DataSet
sequence = (
"name",
"source",
"data_type",)
unlocalize = ("id",)
localize = (
"name",
"source",
"data_type",)
datasets.html
{% load i18n %}
{% load render_table from django_tables2 %}
{% block content %}
{% render_table table %}
{% endblock content %}
This table is rendered as follows:
name
source
data_type
name_en
name_fr
source_en
source_fr
data_type_en
data_type_fr
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
Please let me know what I am missing.

Related

Get the domain name in Twig/Timber to output in WordPress?

When I have an error in my custom WordPress theme I would like to output the webmaster email address which would be webmaster#mydomainname.com but I am a bit baffled on how to do this in Twig/Timber in the most straightforward way: <p class="text-danger fw-bold">PAGE ERROR - Please contact Webmaster at webmaster#{{ #notsure# }}</p>
webmaster#{{ site.url }} just outputs: webmaster#https://mywordpress.local which obviously won't work.
UPDATED: To get by I am using webmaster#{{ site.url[8 :] }} as that strips away the https:// and outputs webmaster#mywordpress.local but seems there should be a cleaner way somehow?
There are two ways to do this:
You can use Advanced custom field. Make a email field and then print that value inside the twig file. Inside advanced custom field, you can add any email you have no need to extract a domain name. For more information follow this reference: https://timber.github.io/docs/guides/acf-cookbook/
Second method is the way you doing is correct but you need to split domain name from site.url using slice method:
{% set website = "https://mywordpress.local" %} //
//calcualting length of string
{% set lengthOfWebsite = website|length %}
//using length here to split the string accordingly. 8is for split "https://" from actual domain name.
{% set domainName = website|slice(8,lengthOfWebsite) %}
webmaster#{{domainName}}
For the first line of code in your case will be:
{% set website = site.url %}
For the last line
webmaster#{{domainName}}
can also be replaced by:
{% set actualDomainName = 'webmaster#' ~ domainName %}
{{actualDomainName}}

Issue with Timber custom page template for Home page in WordPress

I created page-home-page.twig in my view folder of Timber template. However,
1- If the page home-page set as default wordpress home-page, The template file will not work.
2- If the page home-page set as normal page content, then now the template will be okay.
Can any one advise me where is the issue?
Please view Timber documentation.
I fixed it by doing the following:
1- In my WordPress theme folder /wp-content/themes/my-theme/ I created an Custom Page Template reference to this guide: Custom Page Templates
Using this code:
<?php
/**
* Template Name: Video Template
* Description: A Page Template for Home page Video CMS.
*/
defined('ABSPATH') or die;
use Timber\Timber;
$gantry = Gantry\Framework\Gantry::instance();
$theme = $gantry['theme'];
// We need to render contents of <head> before plugin content gets added.
$context = Timber::get_context();
$context['page_head'] = $theme->render('partials/page_head.html.twig', $context);
$post = Timber::query_post();
$context['post'] = $post;
Timber::render(['page-' . $post->post_name . '.html.twig', 'page-hvideo.twig'], $context);
Where Timber::render(['page-' . $post->post_name . '.html.twig', 'page-hvideo.twig'], $context); pointing us to the new Timber page-hvideo.twig file under theme/view folder.
2- And in my Timber template view folder I added this file page-hvideo.twig with the following code:
{% extends "partials/page.html.twig" %}
{% set twigTemplate = 'single.html.twig' %}
{% set scope = 'single' %}
{% block content %}
<div class="platform-content">
<div class="content-wrapper">
<section class="entry">
{% include ['partials/content-' ~ scope ~ '-home-video.html.twig', 'partials/content-home-video.html.twig'] %}
</section>
</div> <!-- /content-wrapper -->
</div>
{% endblock %}
Where {% include ['partials/content-' ~ scope ~ '-home-video.html.twig', 'partials/content-home-video.html.twig'] %} Will manage the home page custom template from view/partials folder under Timber theme folder as normal.
I hope this will help anyone else.
Thank you so much for everything you provided here.
EDITED:
Please be sure after you add your Home Page custom page template to select it from your post editing screen in WordPress under Page Attributes section.

How to display the Content type custom fields in twig template?

I am very new to Drupal.
Can anyone tell me how to display the custom fields content type into twig template?
Thank you
I used below code with content and nodes.
{{ content.field_a }}
You'll have to follow these steps :
Step 1) Custom template for a content type
Suppose, you have to create a new template for content type i.e. Article ( machine_name : article ). Just make a copy of node.html.twig and rename with node--article.html.twig
Step 2) Call custom field
In this template, you can display content of your field like {{ content.field_test_field }}
Step 3) Clear Cache
If you're looking to create a full page template for an Article for example, you can create a file called : page--node--article.html.twig
Any custom content type would follow the pattern, for example if the content type is called Machine Product, the template would be: page--node--machine-product.html.twig (1 dash between spaces of the content type name.
For Drupal 8, this is my typical mapping for this filetype :
Blocks:
{{ page.REGIONNAME }}
Title:
{{ node.title.value }}
Taxonomy Terms/Dropdown Selects in Create Content:
{{ node.field_FIELDNAME.0.entity.label }}
General Text Fields:
{{ node.field_FIELDNAME.value }}
Images/Files:
{{ file_url(node.field_FIELDNAME.entity.fileuri')) }}

flask-bootswatch: css is not loading in "/" paths

While working on a project which I build using Flask and a Bootswatch theme which is a books website, letting users to register, log in and then search for books and when a users selects one of results it gives information about that book,but I found out that information page which I implemented wasn't loading the styling of page, contrary to all other pages which are working fine.
My application.py for information page is:
#app.route("/book/<isbn>")
def book(isbn):
# getting the book from database
book = db.execute("SELECT * FROM books WHERE isbn=:isbn", {"isbn": isbn}).fetchone()
if book is None:
return render_template("failure.html", error="Please enter some query", code = "403!")
db.commit()
# giving details to webpage
return render_template("book.html", book = book)
as you can see I am using ISBN (International Standard Book Number), as a path for different books because each book has unique ISBN, thus I assume it is great for making links to different individual book page.
and book.html looks something like this:
<!-- further exptending the layout page -->
{% extends "layout.html" %}
<!-- further extending the title -->
{% block title %}
Books: Book Page
{% endblock %}
<!-- further extending the body -->
{% block body %}
<h3 align="center"> {{book.title}} by {{book.author}}</h3>
<div>
<li>
Title: {{book.title}}
</li>
<li>
Author: {{book.author}}
</li>
<li>
ISBN: {{book.isbn}}
</li>
<li>
Year: {{book.year}}
</li>
</div>
{% endblock %}
Please figure out where I am going wrong.
Problem is in first line here in application.py it should be
#app.route("/<isbn>")
because /book/<isbn> loads css from /book/static/bootstrap.min.css.

Drupal 8 twig multiple images field

I have a content type with a field field_gallery that has multiple images.
I would like to get all these images printed in my twig file: page--front.html.twig. So i want to get these images in my frontpage and not only in their nodes. So far i could get them in their nodes with
{{ file_url(node.field_image.entity.fileuri) }}
but not somewhere else (of course since its using node). Is this possible?
Should i create a preprocessor function for page? Any guidance for this?
Yes, This is possible. This question is have two sub tasks :
1) Creating page--front.html.twig file
For creation of this twig file, you'll have to clone file i.e. page.html.twig and rename it with page--front.html.twig
2) Fetch Raw values of Image fields
You need to update code in my .theme file:
function THEMENAME_preprocess_node(&$variables) {
if ($variables['node']->field_image->entity) {
$variables['image_url'] = $url = entity_load('image_style', 'medium')->buildUrl($variables['node']->field_image->entity->getFileUri());
}
}
Then in page--front.html.twig file I have this:
{% for item in image_url %}
<div class="featured-thumb">
<img src="{{ item }}"/>
</div>
{% endfor %}

Resources