python – Django错误— index()缺少1个必需的位置参数:’pk’

python – Django错误— index()缺少1个必需的位置参数:’pk’,第1张

概述尝试打开路径时出现此错误.它需要我的def中的PK并且我插入它,但问题仍然存在.如果有人可以提供帮助,我会欠你很多! 这是我在浏览器中出现的错误: TypeError at /batches/index() missing 1 required positional argument: 'pk'Request Method: GETRequest URL: http://127.0.0 尝试打开路径时出现此错误.它需要我的def中的PK并且我插入它,但问题仍然存在.如果有人可以提供帮助,我会欠你很多!

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

TypeError at /batches/index() missing 1 required positional argument: 'pk'Request Method: GETRequest URL:    http://127.0.0.1:8000/batches/Django Version: 1.11.1Exception Type: TypeErrorException 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 185Python Executable:  /library/Frameworks/Python.framework/Versions/3.6/bin/python3.6Python Version: 3.6.1Python 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 urlfrom . import vIEwsurlpatterns = [    # /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_literalsfrom .models import Businessnamefrom .models import IndivIDualsfrom .models import Batchesfrom django.shortcuts import renderfrom 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")

先感谢您,
科恩

解决方法 索引视图有两个参数.您编写的URL仅提供请求.您必须将pk作为输入提供,就像详细URL一样 总结

以上是内存溢出为你收集整理的python – Django错误— index()缺少1个必需的位置参数:’pk’全部内容,希望文章能够帮你解决python – Django错误— index()缺少1个必需的位置参数:’pk’所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1193989.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存