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
CodePudding user response:
{% extends 'base.html' %}
{% block content %}
<div >
<div >
<div >
<div >
<h1>Featured Pets</h1>
</div>
</div>
</div>
<div >
{% for adopt in adopts %}
<div >
<div >
<img src="
{{adopt.pet_photo.url}}" alt="Card image cap"
style="min-height:262px; max-height: 262px;">
<div >
<h5 >{{adopt.pet_title}}</h5>
<p >{{adopt.price}}</p>
<p >{{adopt.description | safe}}</p>
<a href="{% url 'adopt_detail' adopt.id %}" >Go somewhere</a>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
CodePudding user response:
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)