2017-07-24 67 views
2

我嘗試打開路徑時出現此錯誤。它需要一個PK在我的def,我插入它,但問題仍然存在。如果有人能幫忙,我會很多欠你的!Django錯誤---索引()缺少1所需的位置參數:'pk'

這是錯誤我在瀏覽器:

TypeError at /batches/ 
index() missing 1 required positional argument: 'pk' 
Request Method: GET 
Request URL: http://127.0.0.1:8000/batches/ 
Django Version: 1.11.1 
Exception Type: TypeError 
Exception Value:  
index() missing 1 required positional argument: 'pk' 
Exception Location: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response, line 185 
Python Executable: /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 
Python Version: 3.6.1 
Python Path:  
['/Users/cohen/Documents/project/sanctions', 
'/Users/cohen/Documents/project/sanctions', 
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip', 
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6', 
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload', 
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages', 
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/PyObjC'] 
Server time: Mon, 24 Jul 2017 10:47:02 +0000 

我分批

from django.conf.urls import url 
from . import views 

urlpatterns = [ 
    # /batches/ 
    url(r'^$', views.index, name='index'), 

    # /batches/2 
    url(r'^(?P<batches_id>[0-9]+)/$',views.detail, name="detail"), 

    # businessname/1 
    url(r'^(?P<businessname_id>[0-9]+)/$',views.index_businessname, name="detail_businessname"), 

    # individuals/1 
    url(r'^(?P<individuals_id>[0-9]+)/$', views.index_individuals, name="detail_individuals"), 
] 

URL和意見:

# -*- coding: utf-8 -*- 
from __future__ import unicode_literals 
from .models import BusinessName 
from .models import Individuals 
from .models import Batches 

from django.shortcuts import render 
from django.http import HttpResponse 

# Create your views here. 
def index(request, pk): 
    all_Batches = Batches.objects.all() 
    html = '' 
    for batch in all_Batches: 
     url = '/batches/' + str(batch.id) + '/' 
     html += '<a href="#"' + url + '">' + str(batch.BatchNumber)+ '</a><br>' 
    return HttpResponse(html) 

def detail(request, batch_id): 
    return HttpResponse("<h2>Details for Batches ID:" + str(batch_id) + "</h2") 


def index_businessname(request): 
    all_BusinessNames = BusinessName.objects.all() 
    html = '' 
    for bn in all_BusinessNames: 
     url = '/businessname/' + str(bn.id) + '/' 
     html += '<a href="#"' + url + '">' + bn.FullName + '</a><br>' 
    return HttpResponse(html) 

def detail_businessnames(request, bn_id): 
    return HttpResponse("<h2>Details for Business Names ID:" + str(bn_id) + "</h2") 

def index_individuals(request): 
    all_individuals = Individuals.objects.all() 
    html = '' 
    for i in all_individuals: 
     url = '/individuals/' + str(i.id) + '/' 
     html += '<a href="#"' + url + '">' + i.FullName + '</a><br>' 
    return HttpResponse(html) 


def detail_individuals(request, i_id): 
    return HttpResponse("<h2>Details for Individual Names ID:" + str(i_id)+ "</h2") 

謝謝你在前進, 科恩

+0

查看預計'pk'從url傳遞,但你的url模式不包括'pk'。 – anuragal

回答

3

索引視圖有兩個參數。您只寫的網址爲request。您必須給pk作爲輸入,就像detail URL

+0

謝謝Ananthu! – Cohen

1

您的網址/批次/沒有參數。所以, 你的索引視圖應

def index(request): 
    # ...... 
+0

謝謝哈倫! – Cohen

3

在您的網址包含pk

更改您的網址是這樣,

url(r'(?P<pk>\d+)/$', views.index, name='index'), 

代替,

# /batches/ 

url(r'^$', views.index, name='index'), 

OR,

,如果你不及格pk到的意見,然後從index視圖刪除pk在下面顯示。

def index(request): 
    all_Batches = Batches.objects.all() 
    html = '' 
    for batch in all_Batches: 
     url = '/batches/' + str(batch.id) + '/' 
     html += '<a href="#"' + url + '">' + str(batch.BatchNumber)+ '</a><br>' 
    return HttpResponse(html) 
+0

謝謝Emil! – Cohen

相關問題