重定向失败 - get_absolute_url()

时间:2012-06-26 23:49:33

标签: python django

我在django应用程序中使用haystack和whoosh for search功能。事情进展顺利,但我面临的一个问题是:在搜索关键字并打印出结果后,当我点击结果(标题)时,它不会将我重定向到我点击的关键字的页面,它只是静态的。我尝试添加get_absolute_url方法。但它不起作用。我错过了什么?我想让get_absolute_url将用户重定向到他们必须点击标题后搜索的关键字的页面。

N:B下面是一个表单,用户发布他们的数据,他们的数据在同一页面上过滤。

模型

      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):
             findme_args = {
              'title': self.title,
              'address': self.address,
              'city': self.city,
              'state': self.state,
              'main_view': self.main_view,
              'side_view': self.side_view
         }
         return ('findme', (), findme_args)

搜索/ 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 %}

查看:

           def findme(request,title='title',address='address',city='city',state='state',main_view='main_view',side_view='side_view'):
               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,'findme':True})
             extra_data_context.update({'Meeks':Meek.objects.filter(user=request.user)})
             return render_to_response('postme.html',extra_data_context,context_instance=RequestContext(request))

网址

    #I created a urls.py file in my project app(testapp) folder
      urlpatterns = patterns('testapp.views',
         url(r'^$', findme, name='findme'),
         url(r'^find/(?P<title>\w+)/(?P<address>\w+)/(?P<city>\w+)/(?P<state>\w+)/(?P<main_view>\w+)/(?P<side_view>\w+)/$', 'findme', name='findme_search'),

      )

     #In my project(test) folder, where the main urls.py is, I include the above url.

          urlpatterns = patterns('',
            url(r'^find/$', include('testapp.urls')),
          #haystack url where you can search
               (r'^search/', include('haystack.urls')),

         ) 

2 个答案:

答案 0 :(得分:0)

get_absolute_url的最后一行应该

         return ('findme_search', (), findme_args)

而不是

         return ('findme', (), findme_args)

答案 1 :(得分:0)

我打赌self.main_viewself.side_view\w+ regexp不匹配。而且,这是图像。你不能把图像放到URL字符串中,你需要从中获取一些字符串。或者,如果它是一个对象(保存在DB中),你可以只为你的URL提供一个参数,即该对象的主键。

您还应该为get_absolute_url方法编写测试。然后,您将能够快速得到实际错误。