Haystack with Whoosh

编程入门 行业动态 更新时间:2024-10-10 01:27:44
本文介绍了Haystack with Whoosh-搜索结果不重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我成功安装了whoosh,并与Haystack一起工作。事情工作正常,但我正面临着一个问题:在搜索关键字并打印出结果后,当我点击结果(标题)时,它不会将我重定向到我点击的关键字的页面,它只是静态的。我尝试添加一个get_absolute_url方法。但是它不起作用我缺少什么?

模型

class Meek 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>搜索< / h2> < form method =getaction =。> < table> {{form.as_table}} < tr>< td>& nbsp;< / td> < td> < input type =submitvalue =Search> < / td> < / tr> < / table> {%if query%} < h3>结果< / h3> {%for page.object_list%} < p> < a href ={{result.object.get_absolute_url}}> {{result.object.title}}< / a> < / p> {%empty%} < p>未找到结果。< / p> {%endfor%} {%if page.has_previous或page.has_next%} < div> {%if page.has_previous%}< a href =?q = {{query}}& amp; amp; amp; amp; page = {{page.previous_page_number}}> {%endif%}& ;上一页{%if page.has_previous%}< / a> {%endif%} {%if page.has_next%}< a href =?q = {{query}}& amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; }}> {%endif%} Next& raquo; {%if page.has_next%}< / a> {%endif%}< / div> {%endif%} {%else%} {#显示一些运行示例查询,也可能是查询语法,还有其他? #} {%endif%} < / form> {%endblock%}

Urlconf

#url对象发布的位置。 (r'^ find / $',findme), #haystack url你可以在哪里搜索(r'^ search /',include('haystack.urls' )),

视图:

def findme(request): extra_data_context = {} #if这个字段什么都没做。 如果request.method ==POST: form = MeekForm(request.POST,request.FILES)如果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))

解决方案

在 get_absolute_url URL被称为 findme ,你给它五个参数。在您的网址配置中,URL位于此处:

  • 不会调用 findme (即,您需要具有 name =findme)和
  • 它也不包含任何参数。 li>

例如,以下URL同时具有名称和命名参数(请参阅关于URL的文档了解更多信息):

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

您需要使用参数 main_view , side_view ,地址, city 和 state 所以Django可以正确地反转URL并提供模型的绝对URL。

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

解决方案

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.

更多推荐

Haystack with Whoosh

本文发布于:2023-11-28 20:31:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643856.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Haystack   Whoosh

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!