search - Searching a basic blog with Django -


i writing first django web application fun. still trying understand how django works. created simple blog, add simple search feature well. code have far is:

models.py

from django.db import models  class post (models.model):     title = models.charfield(max_length = 255)     body = models.textfield()     date = models.datetimefield()      def __str__(self):         return self.title 

views.py

from django.shortcuts import render blog.models import post django.db.models import q  def post_list(request):     query = request.get.get("q")     if query:         queryset = post.objects.filter(         q(title__icontains=query) |          q(body__icontains=query)|         q(date__icontains=query))     else:         queryset = post.objects.all()         return render_to_response(request, "blog/blog.html", queryset) 

urls.py

from django.conf.urls import url, include django.views.generic import listview, detailview blog.models import post . import views  urlpatterns = [ url (r'^$', listview.as_view(queryset.order_by('-date'[:25], template_name = "blog/blog.html")), url(r'^(?p<pk>\d+)$', detailview.as_view(model=post, template_name='blog/post.html')), ] 

blog.html

{% extends "personal/header.html" %}  {% block content %}          <form method="get" action="" >         <!-- <label for="id_q" style='color: #daa520; font-size:21px; font-weight: bold; font-family: book antigua, palatino, palatino linotype, palatino lt std, georgia, serif;'>search:</label> -->                     <input type="text" name="q" id="id_q" placeholder='search posts' value='{{request.get.q}}'/>                    <input type="submit" value="search"/>         </form>      {% post in queryset%}         <h5 style='color:#daa520; font-size:20px; font-family: book antigua, palatino, palatino linotype, palatino lt std, georgia, serif;'>{{ post.date|date:"y-m-d" }}<a href = "/blog/{{ post.id }}"> {{ post.title }}</a></h5>     {% endfor %}    {% endblock %} 

this code not work. however, if change queryset object_list in blog.html , queryset=post.objects.all() in urls.py can page shows of blog posts.

my question is, how can page shows blogs query contained in either title, body, or date. seems should pretty simple. i've looked on google , in documentation django extensively seems on head @ point.

thanks in advance.


Comments