'Data from back end not showing in front end using django
I am trying to insert data into my frontend but not sure what I did wrong here. Here is the code...
I don't have any errors showing in my terminal or web browser.
my views.py
from django.shortcuts import render, get_list_or_404
from adopt.models import Adopt
# Create your views here.
def adopt(request):
return render(request, 'adopts/adopt.html')
def adopt_detail(request, id):
single_pet = get_list_or_404(Adopt, pk=id)
data = {
'single_pet': single_pet,
}
return render(request, 'adopts/adopt_detail.html', data)
def search(request):
adopt = Adopt.objects.order_by('-created_date')
if 'keyword' in request.GET:
keyword = request.GET['keyword']
if keyword:
adopt = adopt.filter(description__icontains=keyword)
data = {
'adopt': adopt,
}
return render(request, 'adopts/search.html', data)
my models.py
from django.db import models
from datetime import datetime
from ckeditor.fields import RichTextField
# Create your models here.
class Adopt(models.Model):
pet_gender = (
('F','Female'),
('M', 'Male'),
)
state_choice = (
('AL', 'Alabama'),
('AK', 'Alaska'),
('AZ', 'Arizona'),
('AR', 'Arkansas'),
('CA', 'California'),
('CO', 'Colorado'),
('CT', 'Connecticut'),
('DE', 'Delaware'),
('DC', 'District Of Columbia'),
('FL', 'Florida'),
('GA', 'Georgia'),
('HI', 'Hawaii'),
('ID', 'Idaho'),
('IL', 'Illinois'),
('IN', 'Indiana'),
('IA', 'Iowa'),
('KS', 'Kansas'),
('KY', 'Kentucky'),
('LA', 'Louisiana'),
('ME', 'Maine'),
('MD', 'Maryland'),
('MA', 'Massachusetts'),
('MI', 'Michigan'),
('MN', 'Minnesota'),
('MS', 'Mississippi'),
('MO', 'Missouri'),
('MT', 'Montana'),
('NE', 'Nebraska'),
('NV', 'Nevada'),
('NH', 'New Hampshire'),
('NJ', 'New Jersey'),
('NM', 'New Mexico'),
('NY', 'New York'),
('NC', 'North Carolina'),
('ND', 'North Dakota'),
('OH', 'Ohio'),
('OK', 'Oklahoma'),
('OR', 'Oregon'),
('PA', 'Pennsylvania'),
('RI', 'Rhode Island'),
('SC', 'South Carolina'),
('SD', 'South Dakota'),
('TN', 'Tennessee'),
('TX', 'Texas'),
('UT', 'Utah'),
('VT', 'Vermont'),
('VA', 'Virginia'),
('WA', 'Washington'),
('WV', 'West Virginia'),
('WI', 'Wisconsin'),
('WY', 'Wyoming'),
)
year_choice = []
for r in range(2000, (datetime.now().year+1)):
year_choice.append((r,r))
pet_title = models.CharField(max_length=255)
state = models.CharField(choices=state_choice, max_length=100)
city = models.CharField(max_length=100)
gender = models.CharField(choices=pet_gender, max_length=100, blank=True)
breed = models.CharField(max_length=100)
year = models.IntegerField(('year'), choices=year_choice)
pet_photo = models.ImageField(upload_to='photots/%Y/%m/%d/')
description = RichTextField()
price = models.IntegerField()
created_date = models.DateTimeField(default=datetime.now, blank=True)
def __str__(self):
return self.pet_title
Solution 1:[1]
{% extends 'base.html' %}
{% block content %}
<div class="Featured-container">
<div class="row">
<div class="col-12">
<div class="Featured-header">
<h1>Featured Pets</h1>
</div>
</div>
</div>
<div class="row">
{% for adopt in adopts %}
<div class="col-sm-12 col-md-3 col-lg-3 col-xl-3">
<div class="card Featured-card Pet-card">
<img class=" card-img-top" src="
{{adopt.pet_photo.url}}" alt="Card image cap"
style="min-height:262px; max-height: 262px;">
<div class="card-body">
<h5 class="card-title">{{adopt.pet_title}}</h5>
<p class="card-text">{{adopt.price}}</p>
<p class="card-text">{{adopt.description | safe}}</p>
<a href="{% url 'adopt_detail' adopt.id %}" class="btn btn-primary">Go somewhere</a>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
Solution 2:[2]
You are passing in adopt in your view, but you're referencing adopts in your template. In addition, your queries have the wrong syntax. The code below should work.
def search(request):
adopt = Adopt.objects.all().order_by('-created_date') # CHANGE
if 'keyword' in request.GET:
keyword = request.GET['keyword']
if keyword:
adopts = Adopt.objects.filter(description__icontains=keyword).order_by('-created_date') # CHANGE
data = {
'adopts': adopts, # CHANGE
}
return render(request, 'adopts/search.html', data)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Athena The Katze |
| Solution 2 |
