干草堆与飞快移动 - 搜索结果不重定向(Haystack with Whoosh- Search Results Not Redirecting)
我成功安装了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> </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 }}&page= {{ page.previous_page_number }}">{% endif %}« Previous{% if page.has_previous %}</a> {% endif%} {% if page.has_next %}<a href="?q={{ query }}&page={{ page.next_page_number }}">{% endif %}Next »{% if page.has_next %}</a>{% endif %}</div> {% endif %} {% else %} {# Show some example queries to run, maybe query syntax, something else? #} {% endif %} </form> {% endblock %}
URL配置
#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))
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> </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 }}&page= {{ page.previous_page_number }}">{% endif %}« Previous{% if page.has_previous %}</a> {% endif%} {% if page.has_next %}<a href="?q={{ query }}&page={{ page.next_page_number }}">{% endif %}Next »{% 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))
原文:https://stackoverflow.com/questions/11176625
最满意答案
因为JSON.parse返回Object。 res.end无法写入对象。
Because JSON.parse return the Object. res.end can't write object.
相关问答
更多-
为什么不JSON.parse工作?(Why doesn't JSON.parse work?)[2022-03-15]
您的JSON格式不正确: var text = '[{"a":"w","b","x"},{"a":"y","b":"z"}]'; ^-- This should be a ':' 它应该是: var text = '[{"a":"w","b":"x"},{"a":"y","b":"z"}]'; Your JSON is not formatted correctly: var text = '[{"a":"w","b","x"},{"a":"y","b" ... -
app.all('/push', function(req, res) { console.log(req.body.name); // Lorem console.log(req.body.name); // 18 res.status(200).json({data:req.body); // will give { name: 'Lorem',age:18'} in response }); 您的数据将在req.body中提供 希望能帮助到你 ...
-
你可以简单地使用JSON.parse 。 node.js构建在V8上 ,它提供了全局对象JSON [docs] 。 JSON对象的定义是ECMAScript 5规范的一部分 。 注意 - JSON.parse可以绑定当前线程,因为它是一个同步方法。 所以如果你打算解析大的JSON对象,使用一个流json解析器。 You can simply use JSON.parse. The definition of the JSON object is part of the ECMAScript 5 specif ...
-
您不会将JSON传递给该函数(JSON将是一个字符串,因此以引号结尾/开头)。 你传递的是一个JavaScript对象文字。 看到不同: 对象文字 {"ID":"2","NAME":"John Smith","EMAIL":"johnsmith@domain.com"} JSON字符串 "{\"ID\":\"2\",\"NAME\":\"John Smith\",\"EMAIL\":\"johnsmith@domain.com\"}\" // or, easier than escaping all th ...
-
有了这个输入json通知,你需要得到这样的: var arr = JSON.parse('[' + event.params.querystring.numbers + ']'); 而不是: var arr = JSON.parse('[' + event.numbers + ']'); 或者使身体映射模板保持您想要的方式: { "number": "$input.params('number')" } http://docs.aws.amazon.com/apigateway/latest/deve ...
-
你应该检查回调,它们在这样的情况下非常有用。 这是您的示例的修复程序,但您应该更多地查看它们,它们肯定会帮助您成长为JavaScript开发人员。 var Parent_Object = function() { parent = this; this.element_data; this.initElementData = function() { var request = new XMLHttpRequest(); request.o ...
-
发现这是Node v6.0.0中的一个错误,其中填充数组表示为空数组。 在git上发布此问题 。 它已在版本6.2.0上得到解决。 Found out this was a bug in Node v6.0.0 where a filled array was represented as an empty array. Posted this issue on git. It has been resolved on version 6.2.0 though.
-
如果方法调用失败,通常有助于查看函数的输入是什么。 我建议在JSON.parse()调用之前执行console.log(localStorage.getItem("temporaryArray"))以查看问题所在。 我猜这个问题的根源是,在某些时候你正在调用localStorage.setItem("temporaryArray",value)并且忘记对localStorage.setItem("temporaryArray",value)进行JSON.stringify()。 If a method in ...
-
因为JSON.parse返回Object。 res.end无法写入对象。 https://github.com/joyent/node/issues/1241 Because JSON.parse return the Object. res.end can't write object. https://github.com/joyent/node/issues/1241
-
response.body是原始文本响应。 只要您指定了正确的content-type标头, response.getBody()应该已经返回已解析的JSON响应。 将JS对象发送到JSON.parse导致SyntaxError 。 response.body is the raw text response. response.getBody() should already return a parsed JSON response as long as you have the correct con ...