Nutan Panta
Nutan Panta

Reputation: 69

How to point to last value of database data in django

well, I tried to point to last value using last but it seems it shows to the last character in the data. So what I am trying to do is in data-words='["Front-End Developer", "Java Developer", "Python Developer"]' I have kept these value in database and I want to bring using for template but I don't want a comma after the last value but using for templete a comma comes and it does not work. Any suggestion on how I can point to the last data.

<main id="hero-image" style="background: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7)),
url({{mainimage.image}});background-position: center center;background-size: cover;background-repeat: no-repeat;background-attachment: fixed;">
    <div id="particles-js">
      <div class="shape-grid"></div>
      <div class="intro text-white">
        <div class="hello font-weight-bold">
          Hello, my name is {{about.first_name}} {{about.last_name}} and
        </div>
        <h1 class="text-uppercase font-weight-bold">
          I am a
          <span class="txt-type" data-wait="3000" 
            data-words='[
              {% for description in maindescription %}
                "{{description.desc}}",
              {% endfor %}
            ]'>
          </span>
        </h1>
      </div>
    </div>
  </main>

My views.py

from django.shortcuts import render
from django.views.generic import TemplateView
from .models import *


class index(TemplateView):
template_name = "base/index.html"

# overide get context date method
def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['mainimage'] = MainImage.objects.first()
    context['maindescription'] = MainDescription.objects.all()
    context['about'] = About.objects.first()
    context['portfolios'] = Portfolio.objects.all()
    return context

My models.py

class MainDescription(models.Model):
    desc = models.CharField(max_length=20,verbose_name="Main Description")

admin panel

Upvotes: 0

Views: 59

Answers (2)

ruddra
ruddra

Reputation: 51988

What @tstoev meant is that you can generate a string from MainDescription model's queryset and pass it as context. For example:

# view
class index(TemplateView):
    template_name = "base/index.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['mainimage'] = MainImage.objects.first()
        context['maindescription'] = ','.join([x.desc for x in MainDescription.objects.all()])
        context['about'] = About.objects.first()
        context['portfolios'] = Portfolio.objects.all()
        return context

# template
data-words='[ {{ maindrescription }} ]'>

Upvotes: 2

tstoev
tstoev

Reputation: 1435

render the data words in the view to a string, and pass the string in the context and not the queryset

Upvotes: 2

Related Questions