1

I successfully installed whoosh and made it work with Haystack. Things are working fine but I'm facing one problem which is; after searching for a keyword and it print out the results, when I click on the result(title), It won't redirect me to the page of the keyword I clicked on, it's just static. I tried adding a get_absolute_url method. Yet it's not working. What I'm I missing?

Models

      class Meek(models.Model):
         user=models.ForeignKey(User)
         title=models.CharField(max_length=250, unique=True)
         address=models.CharField(max_length=200)
         city=models.CharField(max_length=200)
         state=models.CharField(max_length=200)
         main_view=models.ImageField(upload_to="photos",blank=True, null=True)
         side_view=models.ImageField(upload_to="photos",blank=True, null=True)
         pub_date=models.DateTimeField()

         def __unicode__(self):
             return self.title


         @models.permalink
         def get_absolute_url(self):
             return ('findme', (), {
                'main_view': self.main_view,
                'side_view': self.side_view,
                'address': self.address,
                'city': self.city,
                'state': self.state})

Search/search.html

               {% block content %}

                 <h2>Search</h2>


               <form method="get" action=".">

                <table>
                  {{ form.as_table }}
                <tr><td>&nbsp;</td>
                <td>
                 <input type="submit" value="Search">
                </td>
                </tr>
                 </table>
               {% if query %}
                 <h3>Results</h3>
              {% for result in page.object_list %}
               <p>
                <a href= "{{ result.object.get_absolute_url }}" >{{ result.object.title }}</a> 
               </p>
                 {% empty %}
                    <p>No results found.</p>
                 {% endfor %}

               {% if page.has_previous or page.has_next %}
                     <div>
                       {% if page.has_previous %}<a href="?q={{ query }}&amp;page= {{ page.previous_page_number }}">{% endif %}&laquo; Previous{% if page.has_previous %}</a>
               {% endif%}

               {% if page.has_next %}<a href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% endif %}Next &raquo;{% if page.has_next %}</a>{% endif %}</div>
               {% endif %}

                 {% else %}

                    {# Show some example queries to run, maybe query syntax, something else? #}

                {% endif %}
                 </form>

               {% endblock %}

Urlconf

          #url where the objects are posted.
          (r'^find/$', findme), 

         #haystack url where you can search
         (r'^search/', include('haystack.urls')),

Views:

           def findme(request):
               extra_data_context={}
                   #if there's nothing in the field do nothing.
               if request.method=="POST":
                  form=MeekForm(request.POST, request.FILES)
                  if form.is_valid():
                     data=form.cleaned_data
                     newmeeks=Meek(
                         user=request.user,
                         pub_date=datetime.datetime.now(),
                         title=data['title'],
                         main_view=request.FILES['main_view'],
                         side_view=request.FILES['side_view'],
                         address=data['address'],
                         city=data['city'],
                         state=data['state'])
                    newmeeks.save()
                extra_data_context.update({'MeekForm':form})
             else:
                 form = MeekForm()
                 extra_data_context.update({'MeekForm':form})
             extra_data_context.update({'Meeks':Meek.objects.filter(user=request.user)})
             return render_to_response('postme.html',extra_data_context,context_instance=RequestContext(request))
Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
picomon
  • 1,479
  • 4
  • 21
  • 38

1 Answers1

0

In get_absolute_url your URL is called findme and you're giving it five parameters. In your URL configuration, the URL is there but:

  • it is not called findme (i.e., you need to have name="findme"), and
  • it also does not contain any parameters.

For example, the following URL has both a name and a named parameter (see documentation on URLs for more information):

(r'^articles/(?P<year>\d{4})/$', 'news.views.year_archive', name="news_year_archive"),

You need to create a similar URL with the parameters main_view, side_view, address, city and state so Django can properly reverse the URL and provide an absolute URL for the model.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • still don't get it. The docs aint helping. Can you please explain further? don't you think I will write another view? – picomon Jun 24 '12 at 13:04
  • I'm not sure what you're asking: Django's `reverse` function is currently unable to find the URL because you're not giving the right name and parameters. If you do that, then Django can determine the URL for each object in the search results. – Simeon Visser Jun 25 '12 at 14:45
  • when user search for a keyword and it return the result, so if the user click on the result, the user should be redirected to a page where it will display every properties of the result. I hope you get my point? if you don't please let me know. Thanks. – picomon Jun 25 '12 at 15:47
  • I understand that, that's what I answered above. The reverse function simply can't compute the URL right now so that's why you don't have correct links. – Simeon Visser Jun 25 '12 at 16:35
  • ok. Will I have to modify my view by doing this: def findme(request, title, main_view): and in my url it will be; url(r'^find(?P\d{4})/(?P\d{2})/$', 'views.find', name='find') please kindly put a newbie through. – picomon Jun 25 '12 at 17:08
  • You have to make sure the parameters in the URL are the same as the parameters you provide in `get_absolute_url`. In your case, there are five parameters so you have to have five parameters in the URL configuration and `get_absolute_url`. I suggest studying the documentation to see how URL configurations work as your names are currently not the same. For example, if you have `"findme"` in `get_absolute_url` then you need to have `name="findme"` in your URL configuration, not `name="find"`. Likewise, if your view is called `findme` then in the URL conf it should be `findme`, not `views.find`. – Simeon Visser Jun 25 '12 at 18:11