我正在尝试将Django Reddit Style Voting实施到我的项目中。过去几天我一直在搜索互联网,试图拼凑如何实现它,但我不清楚如何设置我的urlconf来处理confirm_vote.html
,以及该文件的内容可能是什么?点击投票时收到的错误是:
Generic vote view must be called with either post_vote_redirect, a
"next" parameter in the request, or the object being voted on must
define a get_absolute_url method or property.
与This Part of Django-Votings' Code对应。我的项目设置几乎与Github页面上列出的示例项目相同。
我意识到我需要设置confirm_vote.html页面,并且已经做了很多搜索以找出如何设置urlconf来处理它。我找到了This Blog Post,但它似乎没有回答我的问题。
任何人都可以帮我弄清楚在confirm_vote.html和url正则表达式中放置什么来处理它吗?谢谢你的帮助!
修改
实际上只是意识到confirm_vote.html的urlconf在示例教程中。
(r'^links/(?P<object_id>\d+)/(?P<direction>up|down|clear)vote/?$',
vote_on_object, dict(model=Link, template_object_name='link',
template_name='kb/link_confirm_vote.html',
allow_xmlhttprequest=True)),
但仍然对AttributeError
和confirm_vote.html.
的内容感到困惑,感谢任何想法!
编辑:link_list.html
url(r'^links/?$', object_list, dict(queryset=Link.objects.all(),
template_object_name='link', template_name='links/link_list.html',
paginate_by=15, allow_empty=True)),
{% for link in link_list %}<tr class="{% cycle odd,even %}">
<td class="vote">
{% dict_entry_for_item link from vote_dict as vote %}
{% dict_entry_for_item link from score_dict as score %}
<form class="linkvote" id="linkup{{ link.id }}" action="/links/{{ link.id }}/{% if vote and vote.is_upvote %}clear{% else %}up{% endif %}vote/" method="POST">
<input type="image" id="linkuparrow{{ link.id }}" src="{{ MEDIA_URL }}img/aup{% if vote and vote.is_upvote %}mod{% else %}grey{% endif %}.png">
{% csrf_token %}
</form>
<form class="linkvote" id="linkdown{{ link.id }}" action="/links/{{ link.id }}/{% if vote and vote.is_downvote %}clear{% else %}down{% endif %}vote/" method="POST">
<input type="image" id="linkdownarrow{{ link.id }}" src="{{ MEDIA_URL }}img/adown{% if vote and vote.is_downvote %}mod{% else %}grey{% endif %}.png">
{% csrf_token %}
</form>
</td>
答案 0 :(得分:0)
您的网址(而不是urls.py)中的?next=http://example.com/return_to_this_page_after_vote/
似乎未通过vote_on_object
视图。请检查您的模板或在此处发布。
有关信息: 相反,你可以在重新发明轮子之前使用django-ratings。它易于使用和实施。
修改强>
试试这个:
<form class="linkvote" id="linkup{{ link.id }}" action="/links/{{ link.id }}/{% if vote and vote.is_upvote %}clear{% else %}up{% endif %}vote/?next=/" method="POST">
<input type="image" id="linkuparrow{{ link.id }}" src="{{ MEDIA_URL }}img/aup{% if vote and vote.is_upvote %}mod{% else %}grey{% endif %}.png">
{% csrf_token %}
</form>
<form class="linkvote" id="linkdown{{ link.id }}" action="/links/{{ link.id }}/{% if vote and vote.is_downvote %}clear{% else %}down{% endif %}vote/?next=/" method="POST">
<input type="image" id="linkdownarrow{{ link.id }}" src="{{ MEDIA_URL }}img/adown{% if vote and vote.is_downvote %}mod{% else %}grey{% endif %}.png">
{% csrf_token %}
</form>
我只在行动结束时添加了?next=/
。我不确定它是否会起作用 - 但只是尝试一下。