1

我成功安装了 whoosh 并使它与 Haystack 一起工作。一切正常,但我面临一个问题:搜索关键字并打印出结果后,当我点击结果(标题)时,它不会将我重定向到我点击的关键字的页面,它只是静态的。我尝试添加一个 get_absolute_url 方法。然而它不起作用。我错过了什么?

楷模

      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.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 %}

网址配置

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

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

意见:

           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))
4

1 回答 1

0

get_absolute_url你的 URL 被调用findme并且你给它五个参数。在您的 URL 配置中,该 URL 存在,但是:

  • 它没有被调用findme(即,你需要有name="findme"),并且
  • 它也不包含任何参数。

例如,以下 URL 同时具有名称和命名参数(有关详细信息,请参阅有关 URL的文档):

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

您需要使用参数main_viewside_view、等创建一个类似的 URL address,以便 Django 可以正确地反转 URL 并为模型提供绝对 URL。citystate

于 2012-06-24T11:12:10.280 回答