1

谁能帮我这个。我不明白这个错误

/customerList/ 元组索引处的 IndexError 超出范围

它来自这段代码

self.Customer = get_object_or_404(customer, name__iexact=self.args[0])

我希望能够从 customerForm(F_NAME,L_NAME)和 buildingForm(B_USE,B_TYPE)的两个表单中执行 ListView 一些字段。任何帮助都非常非常非常感谢。感谢你。

Traceback:
File "c:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  111.                         response = callback(request, *callback_args,  **callback_kwargs)
File "c:\Python27\lib\site-packages\django\views\generic\base.py" in view
  48.             return self.dispatch(request, *args, **kwargs)
File "c:\Python27\lib\site-packages\django\views\generic\base.py" in dispatch
  69.         return handler(request, *args, **kwargs)
File "c:\Python27\lib\site-packages\django\views\generic\list.py" in get
  114.         self.object_list = self.get_queryset()
File "f:\iqgit\amon\amonsolution\solution\views.py" in get_queryset
  59.         self.Customer = get_object_or_404(customer, name__iexact=self.args[0])

Exception Type: IndexError at /customerList/
Exception Value: tuple index out of range

视图.py

class customerListView(ListView):
    template_name = "customerList.html",
    model = customer
    context_object_name = "customer_list"

def get_queryset(self):
    self.Customer = get_object_or_404(customer, name__iexact=self.args[0])
    return building.objects.filter(Customer=self.Customer) 

def get_context_data(self, **kwargs):
    context = super(customerListView, self).get_context_data(**kwargs)
    context['building_list'] = building.objects.all()

    return context

表格.py

class customerForm(forms.ModelForm):
    F_NAME = forms.CharField(widget=forms.TextInput()
    L_NAME = forms.CharField(widget=forms.TextInput()  
    EMAIL  = forms.CharField(widget=forms.TextInput()  
    ADD    = forms.CharField(widget=forms.TextInput()
    class Meta:
        model = customer

class buildingForm(forms.ModelForm):
    CUSTOMER     = forms.CharField(widget=forms.TextInput()
    B_FLOORSPACE = forms.CharField(widget=forms.TextInput()
    B_YEAR       = forms.CharField(widget=forms.TextInput() 
    B_USE        = forms.ChoiceField(widget=forms.RadioSelect(), choices=c.Use)
    B_TYPE       = forms.ChoiceField(widget=forms.RadioSelect(), choices=c.Type)
    class Meta:
        model = building
        exclude = ('CUSTOMER',)

网址.py

url(r'^customerList/',customerListView.as_view(), name= "customerList_view"),

客户列表.html

...some of code...

{% for customer in customer_list %}
<tr class = {% cycle "row_even" "row_odd" %}>
<td>{{ customer.id }}</td>
<td class ="name"> <a href=" {% url customer_view customer.id %}">{{ customer.F_NAME }}&nbsp;{{ customer.L_NAME }}</a></td>
<td class ="b_use"> <a href=" {% url customer_view customer.id %}">{{ building.B_USE }}{{ building.B_TYPE }}</a></td>

...some of code...
4

1 回答 1

3

您正在尝试调用self.args[0]从 URL conf 传递的位置参数 ( ),但您尚未将任何位置参数添加到您的实际 url。如果您查看请求的处理方式,您会注意到:

4. 一旦其中一个 [url] 正则表达式匹配,Django 就会导入并调用给定的视图,这是一个简单的 Python 函数。视图被传递一个 HttpRequest 作为它的第一个参数,并且在正则表达式中捕获的任何值作为剩余参数。

您没有将任何参数(无论是位置参数还是命名参数)传递给您的视图,因此self.argsor中没有任何内容self.kwargs。您需要将 URL 更改为以下内容:

url(r'^customerList/(\w+)/',customerListView.as_view(), name= "customerList_view"),

或者理想情况下使用命名参数,例如:

url(r'^customerList/(?P<name>\w+)/',customerListView.as_view(), name= "customerList_view"),

这样您就可以改用self.kwargs.get("name", None)

于 2013-04-10T09:06:25.843 回答