3

所以我一直在考虑为我的对象添加评级功能,并决定使用 Agon-ratings https://github.com/eldarion/agon-ratings

i have followed their usage documentation <http://agon-ratings.readthedocs.org/en/latest/usage.html>
did this with my template,
{% extends "base.html" %}
{% load url from future %}
{% block title %}Don't you jus love easy food{% endblock %}
{% block content %}
{% load agon_ratings_tags %}
<br/>
<br/>

<div>

{% if found_entries %}
 <p>You searched for "{{ query_string }}".</p> 
 {% for restaurant in found_entries %}

 <div>
   <div>
    <table>
     <tr>
        <td>dxtcfyguvhyju</td>


      <td><a href="{% url 'f4l_menu' restaurant.id %}">{{ restaurant.restaurant_name }}</td></a>
     </tr>
     <tr>
      <td>Location</td>
      <td>{{restaurant.address}}</td>
     </tr>
    </table>

 {% endfor %}
{% overall_rating obj as the_overall_rating %}
<div class="overall_rating">{{ the_overall_rating }}</div>
<div id= "user_rating"></div>

{% endif %}
{% if query_string and not found_entries %}
 <p>No results found.</p>
{% else %}
 <p>Type a search query into the box above, and press "Submit" to search.</p>
{% endif %}
   </div>
  </div>
  <div style="clear: both;"></div>
 </div>
</div>
{% user_rating_js request.user obj %}
{% endblock %}

当我加载我的页面时,'str' object has no attribute '_meta' 我显然不明白我哪里出错了。顺便说一句,我是一名初学者 django 开发人员,正在开发我的第一个 django 应用程序

追溯,

Template error:
In template /home/mats-invasion/projects/f4l/templates/public/search.html, error at line 31
   'str' object has no attribute '_meta'
   21 :         


   22 :       <td><a href="{% url 'f4l_menu' restaurant.id %}">{{ restaurant.restaurant_name }}</td></a>


   23 :      </tr>


   24 :      <tr>


   25 :       <td>Location</td>


   26 :       <td>{{restaurant.address}}</td>


   27 :      </tr>


   28 :     </table>


   29 :     


   30 :  {% endfor %}


   31 :  {% overall_rating obj as the_overall_rating %} 


   32 : <div class="overall_rating">{{ the_overall_rating }}</div>


   33 : <div id= "user_rating"></div>


   34 :  


   35 : {% endif %}


   36 : {% if query_string and not found_entries %}


   37 :  <p>No results found.</p>


   38 : {% else %}


   39 :  <p>Type a search query into the box above, and press "Submit" to search.</p>


   40 : {% endif %}


   41 :    </div>


Traceback:
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/mats-invasion/projects/f4l/f4l/f4l/views.py" in search
  64.   return render_to_response('public/search.html',context,context_instance=RequestContext(request))
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/shortcuts/__init__.py" in render_to_response
  20.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
  176.         return t.render(context_instance)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  140.             return self._render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  62.     return self.nodelist.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  123.         return compiled_parent._render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  62.     return self.nodelist.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  123.         return compiled_parent._render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  62.     return self.nodelist.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  281.                 return nodelist.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/agon_ratings/templatetags/agon_ratings_tags.py" in render
  106.             ct = ContentType.objects.get_for_model(obj)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/contrib/contenttypes/models.py" in get_for_model
  32.         opts = self._get_opts(model)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/contrib/contenttypes/models.py" in _get_opts
  20.         return model._meta.concrete_model._meta

Exception Type: AttributeError at /search/
Exception Value: 'str' object has no attribute '_meta'

在views.py中,

def search(request):
    query_string=''
    found_entries=None
    search_fields=('delivery_territory',)
    option=request.REQUEST.get('deliveryType',None)
    if option== 'D':
        start_set=Restaurant.objects.filter(services='DEL')
    elif option == 'P':
        start_set = Restaurant.objects.filter(services='PCK')
    else:
        start_set=Restaurant.objects.all()
    if ('q' in request.GET) and request.GET['q'].strip():
        query_string= request.GET['q']
        entry_query= get_query(query_string,search_fields)
        found_entries= start_set.filter(entry_query).order_by('created_on')
    obj = Restaurant.objects.all()
    context={
        'query_string':query_string,
        'found_entries':found_entries,
        'obj':obj,
    }
    return render_to_response('public/search.html',context,context_instance=RequestContext(request))
4

2 回答 2

4

这似乎obj是一个字符串值。而不是模型实例。请验证obj变量的值。

编辑 :

使用文档中给出的解释是针对详细视图(或多或少有一个主要对象)。

如果您愿意在其中显示每家餐厅的评分,则found_entries可能需要进行一些更改。(如果没有请提及)

该类在这一行overall_rating中是硬编码的。为了能够在搜索结果中对每家餐厅进行评分,您必须进行更改。

于 2013-02-05T12:54:12.160 回答
1

只需将您的 2 个模板标签更改为如下所示: {% overall_rating yourappmodel as the_overall_rating %}{% user_rating_js request.user yourappmodel %}

于 2013-02-22T23:43:33.733 回答