【问题标题】:Reverse for 'view_item' with arguments '('',)' and keyword arguments '{}' not found找不到带有参数“(”,)”和关键字参数“{}”的“view_item”的反向
【发布时间】:2017-06-17 05:55:11
【问题描述】:

我正在使用 django 1.4 创建一个购物网站,我将我的产品显示为列表视图,当我尝试将产品的 id 传递给 url 模式时,我不断收到以下错误。所以我的产品列表没有加载。

未找到带有参数“('',)”和关键字参数“{}”的“view_item”的反向操作。

list_products.html

<td class="vertical-center"><a href="{% url view_item item_id %}">{{ item_id }}</a></td>

urls.py

url(r'^view_item/(?P<item_id>\w+)', 'view_item', name="view_item"), 

【问题讨论】:

  • 您需要在模板上下文中包含item_id。消息arguments '('',)' 告诉您上下文中缺少item_id。您还没有显示视图,所以我们无法提供更多帮助。
  • 为什么要使用旧的、不受支持的 Django 版本创建新网站?您应该升级到受支持的版本。
  • @kbnk 非常正确。我将删除之前的评论,因为它对这里的问题没有帮助。感谢您指出这一点
  • 我正在遍历列表视图中的记录,那么在遍历列表视图中的记录之前如何传递 ID
  • 在这种情况下,请显示更多您的模板。如果您没有从视图中传入item_id,您仍然需要确保在使用它时它可以作为(本地范围的)变量使用。

标签: python django


【解决方案1】:

有几种方法可以路由 url,First Class Based 然后 Function Based

举个例子:

模型.py

from django.db import models
from django.core.urlresolvers import reverse

# Create your models here.
class Item(models.Model):
    name = models.CharField(max_length = 120)
    description = models.TextField(blank = True, null = True)
    price = models.DecimalField(decimal_places = 2, max_digits = 20)

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('item_detail', kwargs = {'pk' : self.pk})
# this function will return  the url for item_detail with primary key
# url(r'^items/(?P<pk>\d+)/$', ItemDetailView.as_view(), name = 'item_detail'),
#here we are matching a url to pk and specify that id is a digit
# then set the class Based View
# then set the name of the url
# Primary key is automaticaly added by django, to every object 
# it is same as id.

Views.py:基于类

from django.shortcuts import render
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView

# Create your views here.
from .models import Item

class ItemListView(ListView):
    model = Item
    template_name = 'list_view.html'

    def get_context_data(self, **kwargs):
        context = super(ItemListView, self).get_context_data(**kwargs)
        return context

class ItemDetailView(DetailView):
    model = Item
    template_name = 'detail_view.html'

    def get_context_data(self, **kwargs):
        context = super(ItemDetailView, self).get_context_data(**kwargs)
        return context

urls.py

from items.views import *

urlpatterns = [
    url(r'^items/$', ItemListView.as_view(), name = 'items'),
    url(r'^items/(?P<pk>\d+)/$', ItemDetailView.as_view(), name = 'item_detail'),
]

list_view.html!!!!!!你的答案在这里!!!

{% extends "base.html" %}

{% block content %}

<br><br><br>
{% for item in item_list%}
<a href='{{item.get_absolute_url}}'><p>{{item.name}}</p></a>
<!-- these wil work exactly the same. How ever the first version is the most reliable. -->

<a href='{% url "item_detail" pk=item.pk %}'><p>{{item.name}}</p></a>
<!-- These are also Identical. How ever For class BASED VIEWS you MUST use
either Primary Key or a SLUG!!! -->

<a href='{% url "item_detail" pk=item.id %}'><p>{{item.name}}</p></a>

{% endfor%}


{% endblock content %}

detail_view.html

{% block content %}
<br><br><br>
<br><br><br>

<table class="table">

<tr>
<td>{{object.id}}</td>
<td>{{object.name}}</td>
<td>{{object.pk}}</td>
<td>{{object.description}}</td>
<td>{{object.price}}</td>
</tr>
</table>
{% endblock content %}

Views.py 基于函数

def item_detail_view_func(request, id):
    item_instance = Item.objects.get(id =id)
    template = 'detail_view.html'
    context = {}
    context['object'] = item_instance
    return render(request, template, context)

网址.py

url(r'^items/(?P<id>\d+)/$', item_detail_view_func, name = 'item_detail'),

list_view.html!!!!!!你的答案在这里!!!

{% for item in item_list%}
<!-- this version of urls will only work with function base views!!! -->
<a href='{% url "item_detail" id=item.id %}'><p>{{item.name}}</p></a>
{% endfor%}

【讨论】:

    猜你喜欢
    • 2010-12-22
    • 2013-12-07
    • 2017-04-19
    • 2015-08-29
    • 2014-05-29
    • 2013-07-25
    • 2019-11-06
    • 2012-07-08
    相关资源
    最近更新 更多