NoReverseMatch在/为什么会发生这种情况?

时间:2015-12-09 16:14:15

标签: python django

之前我遇到过这个错误,但我无法修复它,所以我又回到了之前的git状态。现在我必须解决这个问题,为什么这个错误会继续发生?这甚至意味着什么?它来自index.html {%url vote_for_post%}这是我的代码,提前感谢。

views.py

def index(request):

    categories = Category.objects.order_by('likes')
    latest_posts = list(Post.objects.order_by('pub_date')[:50])
    hot_posts = sorted(latest_posts, key=lambda x: x.hot(), reverse=True)
    controversial_topics = sorted(latest_posts, key=lambda x: x.controversy(), reverse=True)

    context_dict = {
        'latest_posts': latest_posts,
        'categories': categories,
        'hot_posts': hot_posts,
        'controversial_topics':controversial_topics
    }
    return render(request, 'main/index.html', context_dict)

def vote_for_post(request, category_name, post_id):
    category = get_object_or_404(Category, name=category_name)
    post = get_object_or_404(Post, id=post_id, category=category)

    if request.POST['type'] == 'upvote':
        post.upvotes += 1
    if request.POST['type'] == 'downvote':
        post.downvotes += 1

    post.save()

    if request.POST['referer'] == 'index':
        return HttpResponseRedirect(reverse('main:index'))
    if request.POST['referer'] == 'category':
        return HttpResponseRedirect(reverse('main:category', args=(category.name,)))

urls.py

urlpatterns = [
    url(r'^$', views.index, name='index'),

    #url(r'^add_post/', views.add_post, name='add_post'),
    url(r'^add_post/$', PostCreateView.as_view(), name='post-add'),
    url(r'^vote/$', views.vote_for_post, name='vote_for_post'),
    url(r'^(?P<slug>[\w|\-]+)/edit/$', PostUpdateView.as_view(), name='post-edit'),
    url(r'^(?P<slug>[\w|\-]+)/delete/$', PostDeleteView.as_view(), name='post-delete'),


    url(r'^add_category/', views.add_category, name='add_category'),
    url(r'^(?P<slug>[\w|\-]+)/$', views.post, name='post'),

    url(r'^category/(?P<category_name_slug>[\w\-]+)/$', views.category, name='category'),

]

的index.html

<div class="row placeholders">
            <div class="col-sm-8">
                <div class="row">
                    {% if hot_posts %}
            {% for vote in hot_posts %}
                <article>
                        <div class="vote">
                            <form method="post" style="display: inline"
                                action="{% url vote_for_post %}">
                                <input type="hidden" value="upvote" name="type">
                                <input type="hidden" value="index_page" name="referer">
                                <button type="submit">+</button>
                                {% csrf_token %}
                            </form>
                            <form method="post" style="display: inline"
                                action="{% url 'main:vote' vote.category.name vote.id %}">
                                <input type="hidden" value="downvote" name="type">
                                <input type="hidden" value="index_page" name="referer">
                                <button type="submit">-</button>
                                {% csrf_token %}
                            </form>
                        </div>


                </article>
                <hr>
            {% endfor %}
        {% else %}
            <p>No topics are available.</p>
        {% endif %}
    </div>
    </div>
    </div>

2 个答案:

答案 0 :(得分:0)

来自Django文档:https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#url

{% url 'some-url-name' v1 v2 %}

在您的代码中,您应该将' '添加到网址模板标记中的名称。

<form method="post" style="display: inline" action="{% url vote_for_post %}">

应该是:

<form method="post" style="display: inline" action="{% url 'vote_for_post' %}">

关于NoReverseMatch at / u'main' is not a registered namespace.

删除'main:'中的(reverse('main:index'))和views.py中的reverse('category', args=(category.name,))

if request.POST['referer'] == 'index':
        return HttpResponseRedirect(reverse('index'))
    if request.POST['referer'] == 'category':
        return HttpResponseRedirect(reverse('category', args=(category.name,)))

答案 1 :(得分:0)

正如我所见,&#34; add_post&#34;,&#34;投票&#34;和&#34; add_category&#34;与你的&#34; slug&#34;模糊不清由于正则表达式匹配。

  

IDlist=mydataframe[!duplicated(mydataframe[,"ID"],),"ID"] Datelist=mydataframe[!duplicated(mydataframe[,"date"],),"date"] newdf=data.frame(matrix(NA, nrow = length(Datelist), ncol = length(IDlist))) rownames(newdf)=Datelist colnames(newdf)=IDlist for(i in 1:nrow(mydataframe)){ mydataframe[newdf[i,"date"],newdf[i,"ID"]]=newdf[i,"consumption"] }

     

url(r'^(?P<slug>[\w|\-]+)/$', views.post, name='post'),

     

url(r'^vote/$', views.vote_for_post, name='vote_for_post'),

     

url(r'^add_post/$', PostCreateView.as_view(), name='post-add'),

尝试为您的网址提供更好的标识符。

e.g。

url(r'^add_category/', views.add_category, name='add_category'),

如果这有帮助,请告诉我。