【问题标题】:Django: IndexError at /customerList/ tuple index out of rangeDjango:/customerList/ 元组索引处的 IndexError 超出范围
【发布时间】:2013-04-10 08:47:12
【问题描述】:

谁能帮我解决这个问题。我不明白这个错误

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

它来自这段代码

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

【问题讨论】:

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


    【解决方案1】:

    您正在尝试调用从 URL conf 传递的位置参数 (self.args[0]),但您尚未将任何位置参数添加到您的实际 url。如果您查看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)

    【讨论】:

    • thks @Timmy O'Mahony :)...更改并阅读参考文献。感谢您指出这一点:) 但为什么我会收到此错误 404 错误,提示找不到该页面?
    • 因为self.kwargs... 中由get_object_or_404 使用的字符串与具有该名称的用户不匹配。通过手动执行查询来检查数据库中是否确实存在具有该名称的用户。类似Customer.objects.filter(name__iexact="bill").
    猜你喜欢
    • 2013-07-28
    • 2018-11-16
    • 2017-07-12
    • 2013-12-16
    • 2021-12-21
    • 2014-08-01
    • 2021-04-19
    • 1970-01-01
    相关资源
    最近更新 更多