1

I want to add my own attribute 'fileName' inside method 'as_view()'

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

Django give's me an arror:

TypeError: CodeResponseView() received an invalid keyword 'fileName'. as_view only accepts arguments that are already attributes of the class.
Dmitry Dronov
  • 350
  • 3
  • 11
  • Does this answer your question? [Django class-based view: How do I pass additional parameters to the as\_view method?](https://stackoverflow.com/questions/11494483/django-class-based-view-how-do-i-pass-additional-parameters-to-the-as-view-meth) – Nalin Dobhal Jan 28 '20 at 10:54
  • How is `CodeResponseView` defined? What does it inherit from? – dirkgroten Jan 28 '20 at 11:00
  • Yes, sorry i found the answer there https://stackoverflow.com/questions/11494483/django-class-based-view-how-do-i-pass-additional-parameters-to-the-as-view-meth . Thx Nalin – Dmitry Dronov Jan 28 '20 at 13:11

2 Answers2

4

The error tells you exactly what you should do:

as_view only accepts arguments that are already attributes of the class

So add fileName as attribute to your class:

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

Now, any url pattern passing fileName to as_view() will work:

path('dialogs/', CodeResponseView.as_view(fileName='Dialogs.py')),
path('alerts/', CodeResponseView.as_view(fileName='Alerts.py')),
dirkgroten
  • 20,112
  • 2
  • 29
  • 42
  • i want to use one view class for many different files -- i dont want to use many view classes for many files – Dmitry Dronov Jan 28 '20 at 11:11
  • You can still do that, my code shows `fileName=''` so it's a defined attribute, it just initializes it as empty string, but you can set it to any value you want in the `as_view()` method. Now in your url, you can do `.as_view(fileName="Dialogs.py")` and in another one you can do `.as_view(fileName="SomethingElse.py")` and it'll work. You don't need to define a different class for every filename. – dirkgroten Jan 28 '20 at 11:13
  • IT WORK! need to asign, and then use in def as "self.fileName" – Dmitry Dronov Jan 28 '20 at 13:10
  • It work only inside def.... I put "Accept answer" and add some commit :) Thx a lot, dirkgroten – Dmitry Dronov Jan 28 '20 at 13:16
3

you can add your attribute. your in case 'filename' as in context.

if you want to pass and use at templates side is help full when you use DetailView,ListView,CreateView,TemplateView etc generic-class-base view

there is two way


1. first way is pass argument in as_view function see here not need to pass at view side or view if you have model you can also pass at urls.py in another keyword arguments model=< Your model name >

urls.py

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

Then you can access filename attribute at your template side like

Your Template file

<h1> My file name is : {{ filename }} </h1>

Output:

My file name is Dialogs.py


2. Second way assign extra_context dictionary in your view class where you define at view.py file

urls.py

path('dialogs/', CodeResponseView.as_view()),

views.py Here you not need to override a get_context_data method for pass filename

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

Then you can access filename attribute at your template side like

Your Template file

<h1> My file name is : {{ filename }} </h1>

Output:

My file name is Your File name


This stuff may help you let me know it's answer is right or wrong .....

l.b.vasoya
  • 1,188
  • 8
  • 23