I want to create a simple list of every article URL in my blog (I have >150).
My current code:
{% for article in blogs['myblog'].articles %}
{{article.url}}
{% endfor %}
The problem is that it only outputs 50 URLs, as per the standard Shopify pagination limit. How could I overcome this? Any solutions using Ajax or otherwise would be highly appreciated.
You can overwrite the pagination limit simply by wrapping the call with the pagination tag and passing the number you want to paginate.
{% paginate blogs['myblog'].articles by 999 %}
{% for article in blogs['myblog'].articles %}
{{article.url}}
{% endfor %}
{% endpaginate %}
PS: Please note that the more articles you have the longer it will take for the DOM to load.
Related
I am trying to create a template with Django to use with all the pages I have in my project. All the pages display similar tables which makes it easier but some of them require an extra check or two. So in writing this one-size-fits-all template, I would like to have this piece of code if it is one type of page:
{% if not package_check %}
<p style="color:red">Package not found, so script did not run. Install package and try again</p>
{% elif count|length %}
<!-- rest of html template -->
{% endif %}
Otherwise, I would like to have this piece of code:
{% if count|length %}
<!-- rest of html -->
{% endif | length %}
Since they are very similar I am wondering if it's possible (and how can I do it) to insert it into the HTML with Javascript when loading the page and make it test for the Django variables in the template tags.
Nothing understandable. Try to write clear.
You want to insert a new template or create a new html element from javascript
if you want include new template
you can do this
{% if not package_check %}
<p style="color:red">Package not found, so script did not run. Install package and try again</p>
{% elif count|length %}
<!-- rest of html template -->
{% include 'youranothertemplate.html' %}
{% endif %}
create new element from javascript
const newElement = document.createElement("h1")
newElement.innerText = "Hai"
document.getElementById("new").append(newElement)
<div id="new">
</div>
I am setting up Paypal on a website and am using liquid to pull the cart value into the Paypal JS.
I have achieved this with the following code:
{% capture paypal_price %}
{%- include 'ecommerce/price_total', format_type: 'formatted' -%}
{% endcapture %}
and
value: "{{ paypal_price | remove:'£' }}"
Functionally it pull through the correct price but as you can see from the image below and error occurs as it pushes it down onto a new line.
Liquid Issue in Console
Is this a known issue and does anyone have a fix for this?
The following amendment to the Liquid fixed it :)
{{ paypal_price | remove:'£' | strip_newlines }}
I want to create a list which is clickable and which would allow me to take a survey when I click it.
{% extends "layout.html" %}
{% block title %}
Take Survey
{% endblock %}
{% block main %}
<div class="list-group">
{ for search in searches }
{{search.topic}}
{ endfor }
</div>
{% endblock %}
When I click it I want it to go to a page which shows the questions of the survey whose topic was being shown.
I have created SQL tables for users,surveys,questions and options. Surveys are linked to users, questions are linked to surveys and options are linked to questions.
I just need a way to access the survey id({{search.id}}) in my take route where I could run a SQL query using the survey id to link everything.
Just a disclaimer, I am a beginner in HTML so please try to explain elaborately.
There are multiple ways that you can do this with html, here are a few of them
create a hidden input with the value that u need, and later when u need it retrieve it from that input
<input type="hidden" id="searchId" name="searchId" value="3487">
add a custom attribute to an element, and later when needed retrieve it from that element
<div class="list-group">
{ for search in searches }
{{search.topic}} data-search-id="{{search.id}}"
{ endfor }
</div>
There are also multiple ways you can do this without html
insert a script that has the search id, and later retrieve that id from javascript
{% block main %}
<div class="list-group">
{ for search in searches }
{{search.topic}}
{ endfor }
<script>
// just make sure that this variable is unique, so u dont cause errors or overwrite another variable by mistake
const MY_APP_SEARCH_ID ={{search.id}}
</script>
</div>
{% endblock %}
You could also generate a unique url for each survey
{{search.topic}}
and then handle that search id from the \take endpoint
Hi I am building a simple blog using Python/Django. In my index.html file, I am trying to show archived posts when a button containing a month is clicked. At the moment, I am just trying to get the id of every post made in that month into a javascript array. For some reason though,it always returns true!
<script type="text/javascript">
function showPosts(month)
{
var posts_in_month=[];
{% for post in all_posts %}
var match = {{ post.pub_date.month }}
{% ifequal match month %}
posts_in_month.push({{ post.id }})
{% endifequal %}
{% endfor %}
}
</script>
I then go on to have a switch statement where I show the contents of the array depending on the month clicked. The code definitely works, when I call an alert(month) it shows up correctly, and when I call alert({{ post.pub_date.month }}) it shows up fine as well. These are usually not the same though, why is it always evaluating the ifequal to true?
You cannot create Javascript variable in python and use it as python's variable.
Replace the match with actual value:
{% ifequal post.pub_date.month month %}
posts_in_month.push({{ post.id }})
{% endifequal %}
In a Django template, how could I refer to the URL. I want to use it in static pages, to avoid having live links to the current page. Is there a way to do this with the Django template language or do I have to use JavaScript to do it?
I would like to do something like
{% if current_url == "/about/" %}
About
{% else %}
<a href='/about/'>About</a>
{% endif %}
I'm using it for a simple blog, so there are no views written for those pages.
I presume by your reference to 'static pages' you mean generic views. Internally, these use RequestContext, so you have access to the request object which is the current HttpRequest. So you can access the current URL with request.path.
{% if request.path == '/about/' %}
...
{% endif %}
Note that this if syntax is Django 1.2+ only - if you're using an older version, you have to do:
{% ifequal request.path '/about/' %}
...
{% endifequal %}
instead of current_url in your example above, you can substitute request.path (assuming you've got django.core.context_processors.request in play). And it'd have to be == not = :o)
I think you can accomplish this with simple template inheritance:
# base.html
{% block contactlink %}<a href='/contact/'>Contact</a>{% endblock %}
{% block aboutlink %}<a href='/about/'>About</a>{% endblock %}
...
# about.html
{% block aboutlink %}About{% endblock %}
# contact.html
{% block contactlink %}Contact{% endblock %}
Of course this only works if you have a separate template for each page, but I'm assuming you do since you said the pages are static. Knowing more about what views you are using (assuming generic view direct_to_template or similar) and your urls.py would help.