0

I am trying to display the title and description from an SQLite database in Django? I have all the data from my database and from views file I have returned it as context.
Here's the model.py file:

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse


# Create your models here.

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()

Here's my views.py file:

def home(request):
    posts = Post.objects.all()
    search_query = request.GET.get('q')
    if search_query:
        posts = posts.filter(
            Q(title__icontains = search_query) |
            Q(content__icontains = search_query)
        )

    context={
        'posts': posts
    }
    return render(request,'blog/home.html', context)

And here the HTML code:-

    {% for post in posts %}
      <div class="slide-description">
          <label class="slide-0">
              <h1 class="text-slide">{{Here i want to print the title 1}}</h1>
              <h5>{{Here I want to print the content 1}}</h5>
              <a href="/" class="readmorebutton">Read More</a>
          </label>
          <label class="slide-1">
              <h1 class="text-slide">{{Here i want to print the title 2}}</h1>
              <h5>{{Here I want to print the content 2}}</h5>
              <a href="/" class="readmorebutton">Read More</a>
          </label>
          <label class="slide-2">
              <h1 class="text-slide">{{Here i want to print the title 3}}</h1>
              <h5>{{Here I want to print the content 3}}</h5>
              <a href="/" class="readmorebutton">Read More</a>
          </label>
          <label class="slide-3">
              <h1 class="text-slide">{{Here i want to print the title 4}}</h1>
              <h5>{{Here I want to print the content 4}}</h5>
              <a href="/" class="readmorebutton">Read More</a>
          </label>
      </div>
    {% endfor %}

Is there any way to get my job done i.e., display one title at a time or like what I am trying to say is, is there something like: post.title[1], post.title[2],.... or anything like this?

2 Answers 2

1

You can simply loop through the posts.

      <div class="slide-description">
         {% for post in posts %}
          <label class="slide-0">
              <h1 class="text-slide">{{post["title"]}}</h1>
              <h5>{{post["content"]}}</h5>
              <a href="/" class="readmorebutton">Read More</a>
          </label>
         {% endfor %}
      </div>
    
Sign up to request clarification or add additional context in comments.

1 Comment

That's not the solution.
0

You can get that object with the specific title or content, like this:

def home(request):
    posts = Post.objects.get(title='that title you want to use')
    posts2 = Post.objects.get(title='that second title you want to use')
    and...
    search_query = request.GET.get('q')
    if search_query:
        posts = posts.filter(
            Q(title__icontains = search_query) |
            Q(content__icontains = search_query)
            )

     context={
        'posts': posts
         }
     return render(request,'blog/home.html', context)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.