Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.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
如何在django as_view()方法中添加自己的属性?_Django - Fatal编程技术网

如何在django as_view()方法中添加自己的属性?

如何在django as_view()方法中添加自己的属性?,django,Django,我想在方法“as_view()”中添加我自己的属性“fileName” Django给我一个Aror: TypeError: CodeResponseView() received an invalid keyword 'fileName'. as_view only accepts arguments that are already attributes of the class. 该错误准确地告诉您应该做什么: as_视图只接受已经是类属性的参数 因此,将fileName作为属性添加到类

我想在方法“as_view()”中添加我自己的属性“fileName”

Django给我一个Aror:

TypeError: CodeResponseView() received an invalid keyword 'fileName'. as_view only accepts arguments that are already attributes of the class.

该错误准确地告诉您应该做什么:

as_视图只接受已经是类属性的参数

因此,将
fileName
作为属性添加到类中:

class CodeResponseView(View):
    fileName = ''

    # rest of view code can now use the fileName attribute

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['file'] = self.fileName
        return context
现在,任何将
fileName
传递到
as\u view()
的url模式都将起作用:

path('dialogs/', CodeResponseView.as_view(fileName='Dialogs.py')),
path('alerts/', CodeResponseView.as_view(fileName='Alerts.py')),

该错误准确地告诉您应该做什么:

as_视图只接受已经是类属性的参数

因此,将
fileName
作为属性添加到类中:

class CodeResponseView(View):
    fileName = ''

    # rest of view code can now use the fileName attribute

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['file'] = self.fileName
        return context
现在,任何将
fileName
传递到
as\u view()
的url模式都将起作用:

path('dialogs/', CodeResponseView.as_view(fileName='Dialogs.py')),
path('alerts/', CodeResponseView.as_view(fileName='Alerts.py')),

可以添加属性。您的案例'filename'与上下文相同

如果您想传递并使用at templates,则在使用详细视图列表视图创建视图模板视图通用类基本视图时,会得到完整的帮助

有两条路

path('dialogs/', CodeResponseView.as_view(extra_context={'fileName':'Dialogs.py')),
path('dialogs/', CodeResponseView.as_view()),

1。第一种方法是在as_view函数中传递参数,请参见此处,如果您有模型,则无需在视图侧或视图中传递参数,您也可以在另一个关键字参数model=

url.py

path('dialogs/', CodeResponseView.as_view(extra_context={'fileName':'Dialogs.py')),
path('dialogs/', CodeResponseView.as_view()),
然后您可以访问模板端的filename属性,如

您的模板文件

class CodeResponseView(DetailView):
    extra_context={'filename':'Your File name'}
    model=models.<model-name> # here your model name 

我的文件名是:{{{filename}}

输出:
我的文件名是Dialogs.py


2.第二种方法是在视图类中指定额外的上下文字典,您可以在view.py文件中定义该字典

url.py

path('dialogs/', CodeResponseView.as_view(extra_context={'fileName':'Dialogs.py')),
path('dialogs/', CodeResponseView.as_view()),
views.py这里您不需要覆盖passfilename的获取上下文数据的方法

class CodeResponseView(DetailView):
    extra_context={'filename':'Your File name'}
    model=models.<model-name> # here your model name 

class CodeResponseView(详细视图):
额外上下文={'filename':'Your File name'}
模型=模型这是您的型号名称
然后您可以访问模板端的filename属性,如

您的模板文件

class CodeResponseView(DetailView):
    extra_context={'filename':'Your File name'}
    model=models.<model-name> # here your model name 

我的文件名是:{{{filename}}

输出:
我的文件名是您的文件名


  • 这里我放置了一个额外上下文和预定义变量的django文档链接

这些东西可以帮助你让我知道它的答案是对的还是错的。…

你可以添加你的属性。您的案例'filename'与上下文相同

如果您想传递并使用at templates,则在使用详细视图列表视图创建视图模板视图通用类基本视图时,会得到完整的帮助

有两条路

path('dialogs/', CodeResponseView.as_view(extra_context={'fileName':'Dialogs.py')),
path('dialogs/', CodeResponseView.as_view()),

1。第一种方法是在as_view函数中传递参数,请参见此处,如果您有模型,则无需在视图侧或视图中传递参数,您也可以在另一个关键字参数model=

url.py

path('dialogs/', CodeResponseView.as_view(extra_context={'fileName':'Dialogs.py')),
path('dialogs/', CodeResponseView.as_view()),
然后您可以访问模板端的filename属性,如

您的模板文件

class CodeResponseView(DetailView):
    extra_context={'filename':'Your File name'}
    model=models.<model-name> # here your model name 

我的文件名是:{{{filename}}

输出:
我的文件名是Dialogs.py


2.第二种方法是在视图类中指定额外的上下文字典,您可以在view.py文件中定义该字典

url.py

path('dialogs/', CodeResponseView.as_view(extra_context={'fileName':'Dialogs.py')),
path('dialogs/', CodeResponseView.as_view()),
views.py这里您不需要覆盖passfilename的获取上下文数据的方法

class CodeResponseView(DetailView):
    extra_context={'filename':'Your File name'}
    model=models.<model-name> # here your model name 

class CodeResponseView(详细视图):
额外上下文={'filename':'Your File name'}
模型=模型这是您的型号名称
然后您可以访问模板端的filename属性,如

您的模板文件

class CodeResponseView(DetailView):
    extra_context={'filename':'Your File name'}
    model=models.<model-name> # here your model name 

我的文件名是:{{{filename}}

输出:
我的文件名是您的文件名


  • 这里我放置了一个额外上下文和预定义变量的django文档链接

这些东西可以帮助你让我知道它的答案是对的还是错的。…

这能回答你的问题吗?如何定义
CodeResponseView
?它是从什么继承来的?是的,很抱歉我在那里找到了答案。这能回答你的问题吗?如何定义
CodeResponseView
?它是从什么继承来的?是的,很抱歉我在那里找到了答案。Thx Nali我想对许多不同的文件使用一个视图类——我不想对许多文件使用许多视图类。您仍然可以这样做,我的代码显示
fileName=''
,因此它是一个已定义的属性,它只是将其初始化为空字符串,但您可以在
as_view()
方法中将其设置为任何您想要的值。现在在您的url中,您可以执行
.as\u view(fileName=“Dialogs.py”)
操作,在另一个url中,您可以执行
.as\u view(fileName=“SomethingElse.py”)
操作。您不需要为每个文件名定义不同的类。它可以工作!需要先签名,然后在def中用作“self.fileName”,它只能在def中工作。。。。我放了“Accept answer”并添加了一些commit:)Thx很多,DirkGrote我想对许多不同的文件使用一个视图类--我不想对许多文件使用许多视图类你仍然可以这样做,我的代码显示
fileName=''
所以它是一个已定义的属性,它只是将其初始化为空字符串,但是,您可以在
as\u view()方法中将其设置为所需的任何值。现在在您的url中,您可以执行
.as\u view(fileName=“Dialogs.py”)
操作,在另一个url中,您可以执行
.as\u view(fileName=“SomethingElse.py”)
操作。您不需要为每个文件名定义不同的类。它可以工作!需要先签名,然后在def中用作“self.fileName”,它只能在def中工作。。。。我把“接受答案”加上一些承诺:)太多了,dirkgroten