Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django TypeError:\uuuu init\uuuuu()接受1个位置参数,但给出了2个_Python_Django - Fatal编程技术网

Python Django TypeError:\uuuu init\uuuuu()接受1个位置参数,但给出了2个

Python Django TypeError:\uuuu init\uuuuu()接受1个位置参数,但给出了2个,python,django,Python,Django,下面是来自views.py的类代码: class Ask(CreateView): template_name = 'f/ask.html' form_class = QuestionForm success_url = '/f/ask/' def get_context_data(self, **kwargs): content = super().get_context_data(**kwargs) return conten

下面是来自
views.py
的类代码:

class Ask(CreateView):
    template_name = 'f/ask.html'
    form_class = QuestionForm
    success_url = '/f/ask/'

    def get_context_data(self, **kwargs):
        content = super().get_context_data(**kwargs)
        return content
这是我的URL.py代码

from django.urls import path, register_converter
from . import views, converter

register_converter(converter.HexConverter, 'hex')

urlpatterns = [
    path('', views.QuestionView),
    path('ask/', views.Ask),
    path('<hex:pk>/', views.QuestionCurrent, name='question_current'),
]
从django.url导入路径,注册\u转换器
从…起导入视图、转换器
寄存器_转换器(converter.HexConverter,'hex')
URL模式=[
路径(“”,视图。问题视图),
路径('ask/',views.ask),
路径(“/”,views.QuestionCurrent,name='question\u current'),
]
它说
\uuuu init\uuuuuuuo()接受1个位置参数,但给出了2个
,但我从书中获取的代码,所以我不认为这是错误的或什么。

添加到
url.py中的路径(因为它们是基于
类的路径):

发件人:

从django.url导入路径,注册\u转换器
从…起导入视图、转换器
寄存器_转换器(converter.HexConverter,'hex')
URL模式=[
路径(“”,视图。问题视图),
路径('ask/',views.ask),
路径(“/”,views.QuestionCurrent,name='question\u current'),
]
致:

从django.url导入路径,注册\u转换器
从…起导入视图、转换器
寄存器_转换器(converter.HexConverter,'hex')
URL模式=[
路径(“”,views.QuestionView.as_view()),
路径('ask/',views.ask.as_view()),
路径('/',views.QuestionCurrent.as_view(),name='question_current'),
]

从文件中:

classmethodas_视图(**initkwargs)

返回接受请求并返回响应的可调用视图:

response=MyView.as\u view()(请求)

返回的视图具有view_class和
view_initkwargs
属性

在请求/响应周期中调用视图时,
setup()
方法将
HttpRequest
分配给视图的请求属性,并将从URL模式捕获的任何位置和/或关键字参数分别分配给args和kwargs属性。然后调用
dispatch()


…以及完整的错误回溯!可能是您正在使用的django的图书版本和django的版本或其他库发生了更改。这不是一个罕见的问题。错误是否指向此
寄存器\u转换器
?否,它不是指向寄存器\u转换器
from django.urls import path, register_converter
from . import views, converter

register_converter(converter.HexConverter, 'hex')

urlpatterns = [
    path('', views.QuestionView),
    path('ask/', views.Ask),
    path('<hex:pk>/', views.QuestionCurrent, name='question_current'),
]
from django.urls import path, register_converter
from . import views, converter

register_converter(converter.HexConverter, 'hex')

urlpatterns = [
    path('', views.QuestionView.as_view()),
    path('ask/', views.Ask.as_view()),
    path('<hex:pk>/', views.QuestionCurrent.as_view(), name='question_current'),
]