Django:/ customerList / tuple索引的IndexError超出范围

时间:2013-04-10 08:47:12

标签: django django-forms django-templates django-views

任何人都可以帮助我。我不明白这个错误

   / 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

views.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

forms.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',)

urls.py

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

customerList.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...

1 个答案:

答案 0 :(得分:3)

您正在尝试调用从URL conf传递的位置参数(self.args[0]),但您尚未向实际网址添加任何位置参数。如果你看how a request is processed,你会注意到:

  

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

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

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

或理想情况下使用named arguments,例如:

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

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