我有一个投票系统,我首先编码没有slug,但我决定使用slug作为帖子,整个投票系统不起作用。我不知道为什么它不起作用,它给了我404错误,表明网址错了,但我很自信我把一切都搞定了。如果您有任何想法,我将不胜感激。谢谢。
views.py
def index(request):
try:
sort = request.GET["sort"].strip()
sort_method = SortMethods[sort]
page = request.GET["page"].strip()
except KeyError:
sort_method = SortMethods.score
page = 1
if sort_method == SortMethods.date:
thread_list = Post.objects.order_by("-pub_date")
else:
thread_list = Post.objects.all()
thread_list = sorted(thread_list, key=lambda x: x.get_score(), reverse=True)
paginator = Paginator(thread_list, 30)
try:
posts = paginator.page(page)
except PageNotAnInteger:
posts = paginator.page(1)
except EmptyPage:
posts = paginator.page(paginator.num_pages)
context = {
"posts": posts,
"pages": paginator.page_range,
"sort": sort_method.name
}
return render(request, "main/index.html", context)
def vote(request, post_id): #here I switch to slug
try:
error_message = "Not a valid request"
is_up = int(request.GET["is_up"].strip())
if is_up == 1 or is_up == 0:
if not request.user.is_authenticated():
error_message = "please login"
else:
post = get_object_or_404(Post, id=post_id) #I switch to slug=slug
try:
vote = post.vote_set.get(user=request.user)
except Vote.DoesNotExist:
post.vote_set.create(user=request.user, is_up=is_up)
else:
if vote.is_up == is_up:
vote.delete()
else:
vote.is_up = is_up
vote.save()
json_data = '{"count":"%s"}' % post.get_vote_count()
return HttpResponse(json_data, content_type="application/json; charset=utf-8")
except KeyError:
json_data = '{"error_message":"%s"}' % "Not a valid request"
return HttpResponseBadRequest(json_data, content_type="application/json; charset=utf-8")
else:
json_data = '{"error_message":"%s"}' % error_message
return HttpResponseBadRequest(json_data, content_type="application/json; charset=utf-8")
urls.py
url(r'^post/(?P<post_id>\d+)/vote/$', 'main.views.vote', name='vote'), #I switch to ?P<slug>\d+
url(r'^(?P<post_id>[\w|\-]+)/$', views.post, name='post'),
# I switch to ?P<slug>
我的index.html
<table class="table">
{% if posts %}
{% for post in posts %}
<tr>
<td class="vert-align"><div>
<a href="/post/{{ post.id }}/vote?is_up=1" class="vote"> #I switch to post.slug for every post.id
<span class="glyphicon glyphicon-chevron-up" aria-hidden="true"></span></a>
<br>
<a href="/post/{{ post.id }}/vote?is_up=0" class="vote">
<span class="glyphicon glyphicon-chevron-down" aria-hidden="true"></span></a>
</div></td>
<td class="vert-align">
<h4 id="vote_count_{{ post.id }}">{{ post.get_vote_count }}</h4>
</td>
<td>
<h4><a href="{% url 'post' post.id %}">{{ post.title }}</a></h4>
<ol class="breadcrumb">
<li><span>{{ post.moderator.username }}</span></li>
<li><span class="time_presentation">{{ post.pub_date | date }}</span></li>
</ol>
</td>
</tr>
{% endfor %}
然后我收到404错误。任何想法将不胜感激。
答案 0 :(得分:1)
请注意此行中的\d+
:
url(r'^post/(?P<slug>\d+)/vote/$', 'main.views.vote', name='vote'),
\d+
表示只需要数字
slug的正则表达式应该是这样的:
url(r'^post/(?P<slug>[-\w]+)/vote/$', 'main.views.vote', name='vote'),
正如本回答https://stackoverflow.com/a/27322151/4724196
中所述希望这有帮助!