Django错误--- index()缺少1个必需的位置参数:"pk" [英] Django Error ---index() missing 1 required positional argument: 'pk'

查看:63
本文介绍了Django错误--- index()缺少1个必需的位置参数:"pk"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试打开路径时出现此错误.它在我的def中需要一个pk,我将其插入,但是问题仍然存在.如果有人可以帮忙,我会欠你很多!

I have this error when try to open a path. It requires a pk in my def and i inserted it, but still the issue is there. If someone could help, i would owe you a lot!

这是我在浏览器中出现的错误:

This is the error i have in browser:

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"),
]

视图:

# -*- 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")

预先感谢您,科恩

推荐答案

在您的网址中包含 pk .

像这样更改您的网址,

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

而不是

# /batches/

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

如果您没有将 pk 传递给视图,则从 index 视图中删除 pk ,如下所示.

if you are not passing pk to views then remove pk from index view as showned below.

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)

这篇关于Django错误--- index()缺少1个必需的位置参数:"pk"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆