Pycharm: Unresolved library 'django_bootstrap5' - django-models

Context: Using pro version of pycharm (2022.3.2) and set up as
a django project. Very new to Django.
{% load django_bootstrap5 %}
<form action="{% url '. . .' %}" method='post'>
{% csrf_token %}
{% bootstrap_form form %}
{% bootstrap_button button_type="submit" content="Submit" %}
</form>
Error message from Pycharm: Unresolved library 'django_bootstrap5'
Although this error is displayed in pycharm, the code runs fine without errors.
Very misleading. Does anyone know how to fix this erroneous error message or is
this a bug?
I tried to run this problem down but did not get anywhere. Finally
decided just to run the project. Everything worked, no issues. The
template is rendering in bootstrap5.

Related

How to Display Functional Pagination in Twig 3 Template

Objective is to add pagination using Twig 3 that displays and functions identically to the pagination used at the bottom of Stack Overflow question feeds:
Researching this in other questions and in outside forums has only yielded results using depreciated twig functions, so asking here in case anyone has any insight on how to do this properly.
I have a twig template that currently displays pagination, but numerically lists and hyperlinks every page in the list without trimming and adding an ellipses the way Stack Overflow does (also it has no dynamic Next/Previous links). For some background, some of the parameter decisions are framed around the API that I'm working with and follows its documentation for HTTP requests to the JSON data. This template is functional but it's not practical from a user oriented design standpoint:
{% set pgonpage = 5 %}
{% set pgofpages = (count/pgonpage) | round(0, 'ceil') %}
<p>{% for i in 1..pgofpages %}
{% if i==urlparam.pg %}
(<strong>{{urlparam.pg}}</strong>)
{% else %}
<a href=?pg={{i}}>{{i}}</a>
{% endif %}
{% endfor %}</p>
For brevity I removed part of the template above that loops the main content, but to clarify the {% set pgonpage = 5 %} tag sets the number of JSON data to be displayed on a given page, and is functional.
This is how the template URL looks:
{% set page = 1 %}
{% if urlparam.pg > 0 %}
{% set page = urlparam.pg %}
{% endif %}
https://api.example.com?per_page=5&page={{page}}
This is the result if on https://actual.example.com?pg=1:

Can I call a jekyll page attribute within a for statement

{% for item in site.data.{{page.ocean}}.section %}
This is not working for me. I'm using page.ocean at other places in the page and it works fine. I'm not sure if I can do this within a for statement. If not, is there a way to work around it?
Try this:
{% assign ocean = page.ocean %}
{% for item in site.data[ocean].section %}
<!-- your code -->
{% endfor %}
Check Liquid's Variable and yes we can access data variable content like an Array through indexing.

Selecting one post to display in a liquid loop

I'm building a jekyll site. I have a loop setup -
{% for article in site.posts limit:5 %}
{% if article.tag == "infographic" %}
<a class="infographic tag" href="/tags/infographics.html">{{ article.tag }}</a>
{% endif %}
{% endfor %}
This loop spits out the latest 5 posts in my infographic tag. I would like the loop to produce only the third latest post. Any ideas on making this happen?
The solution is really difficult for tags. If you use infographic as a category, it could be really simple:
for article in site.categories.infographic | offset: 2 | limit: 1

jinja2 form render does not allow attribute which contains "-"

I am trying to customize the form template base on this tutorial. As I understand, render() just add some attributes to the tag. For example, I add placeholder = "abc" and it works well.
{% call inserttourbus(id = "formAddNewRow" ) %}
<div class="fieldWrapper">
{% if inserttourbus['bustype'].label() %}Bus Type{% endif %}
{{ inserttourbus['bustype'].render(placeholder="abc")|safe }}
{% if inserttourbus['bustype'].errors() %}Not filled yet!{% endif %}
</div>
{% endcall %}
Here is my problem:
- I use bootstrap typeahead for my template so I need to add the following attribute to the inserttourbus textbox
data-provide="typeahead" data-items="4" data-source='["Alabama","Alaska"]'
So it will become
{{ inserttourbus['bustype'].render(placeholder="abc", data-provide="typeahead", data-items="4", data-source='["Alabama","Alaska"]')|safe }}
But the jinja2 engine seems does not accept data-provide, data-items, so on because it contain "-" character. If I changed data-provide to dataprovide, the jinja2 engine can render the code well.
However, in bootstrap typeahead javascript, all variables are defined as data-provide, data-items. If I change them to dataprovide, dataitems, the javascipt stop working.
Please give me a solution:
- How to make jinja2 accept attribute which has "-"
- Other solutions, advices
Check out this snippet for doing this in Flask. I imagine it would work the same way for Django; pass HTML attributes with invalid Jinja2 (Python) syntax inside an ad-hoc dictionary:
{{ inserttourbus['bustype'].render(placeholder="abc",
**{'data-provide':'typeahead',
'data-items':'4',
'data-source':'["Alabama","Alaska"]'}) }}
A Hyphen is used as the subtraction operator in Python. So do not use it in names. You can use it ofcourse in strings.

google app engine comments and check dev environment in html views

I'm working with a basic *.html file in Google App Engine.
How can I put in comments that will not churn out an html comment? E.g. in Rails I would do <%# comment %>
Also, how can I detect it is a development environment (or check if the url is localhost) and hence only trigger a certain block of html? E.g. in Rails I would do:
<% if Rails.env.development? %>
<p>comment visible only in localhost!</p>
<% end %>
Thanks!
As for the comment piece, you can use similar syntax to what you have above:
{# some comment here #}
{{ variable }}
{% for some item in another_variable %}
<p>{{ item }}</p>
{% endfor %}
Regarding the hostname part, I'm not aware of any template built-in that would handle that. I would suggest making this check in your server-side code and passing the result to the template. Here is a question/answer that should accomplish what you need. Pilfering code from that answer, you could define a variable in you code such as (using Python, as I'm not sure which runtime you are using):
dev_server = os.environ['SERVER_SOFTWARE'].startswith('Development')
You could then reference this in your template (assuming you pass the variable into the template as dev_server) as follows:
{% if dev_server %}
<p>comment visible only in localhost!</p>
{% endif %}
If you want to have conditional code using Java on App Engine, you could write your html as a JSP file. Then you can use conditional blocks, and have comments that don't show up in the final output e.g.,
<%-- This comment will get removed by the JSP compiler --%>
<!-- This is a regular html comment and will survive the JSP compiler untouched -->
<p>Just some ordinary html in a JSP file here...</p>
<h1>Hello StackOverflow!</h1>
<% if ( isSayGoodbye ) { %>
<h3>Goodbye!</h3>
<% } %>
As for testing if you are on AppEngine vs. dev environment, check these (Java) docs from Google: https://developers.google.com/appengine/docs/java/runtime#The_Environment
if (SystemProperty.environment.value() ==
SystemProperty.Environment.Value.Production) {
// The app is running on App Engine...
}

Resources