这是我的代码 views.py
def search(request):
if request.method == 'GET':
try:
q = request.GET.get('search_box',None)
posts = Listing.objects.filter(title__contains=q,is_live=1) | \
Listing.objects.filter(street_address__contains=q,is_live=1) | \
Listing.objects.filter(city__contains=q,is_live=1) | \
Listing.objects.filter(state=q,is_live=1) | \
Listing.objects.filter(property_class__contains=q,is_live=1) | \
Listing.objects.filter(sale_or_lease__contains=q,is_live=1)
return render_to_response('search/results.html',{'posts': posts,'q': q})
except KeyError:
return redirect('home')
results.html:
<div class="container" style="width:20%; float:right; text-align:center; overflow:auto;">
{% for Listing in posts %}
<a href="{% url 'post_view' Listing.pk %}"><img style="width: 384px; height: 216px;" alt="Thumbnail"
src="{{ MEDIA_URL }}{{ Listing.thumbnail }}"/></a>
<p style="color:black;">{{ Listing.title }}</p>
<p style="color:black;">Sale or Lease: {{ Listing.sale_or_lease }}</p>
<p style="color:black;">Class: {{ Listing.property_class }}</p>
<p style="color:black;">Square Feet: {{ Listing.square_feet }}</p>
{% if Listing.price %}
<p style="color:black;">Price: ${{ Listing.price|linebreaksbr }}</p>
{% endif %}
{% if Listing.price_per_square_foot_per_year %}
<p style="color:black;">Price per SqFt/yr:
${{ Listing.price_per_square_foot_per_year|linebreaksbr }}</p>
{% endif %}
<p style="color:black;"> City: {{ Listing.city }},{{ Listing.state }}</p>
<hr>
{% endfor %}
</div>
对于上述代码,不输出Listing.thumbnail,仅占位符文本。这是我正在使用的代码示例。
views.py
def preview(request,pk):
posts = Listing.objects.all().filter(is_live=1)
preview = get_object_or_404(Listing,pk=pk)
attorneys = Attorneys.objects.all().filter(state=preview.state) | \
Attorneys.objects.all().filter(city=preview.city)
lenders = Lenders.objects.all().filter(state=preview.state) | \
Lenders.objects.all().filter(city=preview.city)
developers = Developers.objects.all().filter(state=preview.state) | \
Developers.objects.all().filter(city=preview.city)
context = {'posts': posts,'preview': preview,'pk': preview.pk,'attorneys': attorneys,'lenders': lenders,'developers': developers}
return render(request,'preview.html',context)
preview.html
<div class="container" style="width:20%; float:right; text-align:center;">
<a href="{% url 'post_view' pk %}"><img style="width: 384px; height: 216px;" alt="Thumbnail"
src="{{ MEDIA_URL }}{{ preview.thumbnail }}"/></a>
<p style="color:black;">{{ preview.title }}</p>
<p style="color:black;">Sale or Lease: {{ preview.sale_or_lease }}</p>
<p style="color:black;">Class: {{ preview.property_class }}</p>
<p style="color:black;">Square Feet: {{ preview.square_feet }}</p>
{% if preview.price %}
<p style="color:black;">Price: ${{ preview.price }}</p>
{% endif %}
{% if preview.price_per_square_foot_per_year %}
<p>Price per SqFt/yr: ${{ preview.price_per_square_foot_per_year }}</p>
{% endif %}
<p style="color:black;"> City: {{ preview.city }},{{ preview.state }}</p>
...
</div>
感谢任何帮助,我认为它可能与render_to_response有关,但我不确定。