Slicing Internal JSON Data

I have an api content that will get information inside the data of the api so I accessed that one like this api_list api_content['Data'][:12] this is used to loop all the images, url, body, etc in the template.html, the problem is that content of the body is not uniform so I will like to limit the content of the body to uniform number, something like

{% for a in api_list %}
{{a.body[:300]}}//this is not working at all with error message
{%endfor%}

I want to know how to limit the content of the body to the expected number. Please how can I do this?

See the slice filter in the docs

1 Like

Thank you very @KenWhitesell I noticed that this works very well when working in the local server, but on production, this isn’t working again in the production(even when the debug = True). Please what is wrong? After updating my code with my version control, it is actually reflecting as expected.

There’s not enough information here to even being to guess.

I’d probably need to see the view processing the data, the full template being rendered, a more complete description of what’s not working (including any error messages / tracebacks - complete, not edited or trimmed), and enough data to understand how it maps to the template.

This is the view

def index(request):

    api_request = requests.get(

        "https://min-api.cryptocompare.com/data/v2/news/?lang=EN"+'&api_key='+api_key)

    api_content = json.loads(api_request.content)

    content = {

        'crypto_api': api_content['Data'][:12],

    }

    templates = 'index.html'

    return render(request, templates, content)

This is for the templates

{% for crpt in crypto_api %}

          <div class="col-lg-4  col-md-6 d-flex align-items-stretch" data-aos="fade-up">

            <article class="entry">

              <div class="entry-img img-thumbnail">

                <img src="{{crpt.imageurl}}" alt="" class="img-fluid">

              </div>

              <h2 class="entry-title">

                <a href="{{crpt.url}} ">{{crpt.title}}</a>

              </h2>

              <div class="entry-content">

                <p>

                  **{{crpt.body|slice:":200" |safe}}**

                </p>

                <div class="read-more">

                  <a href="{{crpt.url}}">Read More</a>

                </div>

              </div>

            </article>

          </div>

        {% endfor %}

this actually works perfectly on localhost, but on production (which have exactly the same code) it’s not working, the name of the website is techwagon.org

What is not working? What is happening that isn’t what you’re expecting to see?

Oh I think I really have issue with cache, when I cleaned it now I noticed that everything is working perfectly, thanks a lot sir.