【问题标题】:django 1.6 url tag errordjango 1.6 url标签错误
【发布时间】:2014-02-17 11:03:38
【问题描述】:

我的 app1 应用程序有问题:

Reverse for 'app1.views.add_content' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['/add-content/(\\d+)/$']

观看次数

@login_required
def add_content(request, category_id):
    form = ContentForm()
    category = get_object_or_404(Category, pk=category_id)

    if request.method == 'POST':
        form = ContentForm(request.POST)

        if form.is_valid():

            content = Content()
            content content_code = form.cleaned_data['content_code']
            content.product_versions = form.cleaned_data['product_versions']
            content.category = category
            content.creator = request.user

            content.save()

            return HttpResponseRedirect(reverse('category-detail', args=(category.id, )))

    return render_to_response('add_content.html', {
            'form': form,
            'category': category,
        }, context_instance=RequestContext(request))

网址

url(r'^/add-content/(\d+)/$', 'app1.views.add_content', name='add_content'),

在模板中我在这一行有错误

<a id="add_content" class="button" href="{% url 'app1.views.add_content' pk %}">Add New Content</a>

有什么问题?

【问题讨论】:

  • 看起来pk 没有在您的模板中定义。我没有看到您将pk 传递给render_to_response 中的模板。也许你的意思是category.pk 或类似的东西?

标签: django django-1.6


【解决方案1】:

哦,我错过了将内容添加为添加类别,

你的错误在这里:

{% url 'app1.views.add_content' pk %}

应该是这样的:

{% url 'app1.views.add_content' category.pk %}

【讨论】:

  • 我尝试依次添加:category.pk、category.id、category_id,但报错
  • 在你的网址中你应该有:url(r'^/add-content/(?P&lt;category_id&gt;\d+)/$', 'app1.views.add_content', name='add_content'),
  • 将您的解决方案添加到使用上述所有方法的 url 和模板中,但有相同的错误((
  • 未找到带有参数 '('',)' 和关键字参数 '{}' 的 'app1.views.add_content' 的反向操作。尝试了 1 种模式:['/add-content/(?P\\d+)/$']
  • 您没有传递任何参数,您确定您的模板中定义了类别吗?
猜你喜欢
  • 2012-09-10
  • 2014-04-23
  • 2012-11-08
  • 1970-01-01
  • 2014-07-17
  • 1970-01-01
  • 2015-03-31
  • 2011-10-05
  • 1970-01-01
相关资源
最近更新 更多