Alex Tsirikidze
Alex Tsirikidze

Reputation: 21

missing 1 required positional argument: 'request'

here is my view.py

class categAdmin(admin.ModelAdmin):

    change_form_template = 'category_forms.html'
    list_display = ['title']
    model = Category
    fields = ['status','title','category_post','body', 'photo', 
    'url','slider','Gallery','lists','pk_tree','video','maps']

    # def render_change_form(self, request, context,  **kwargs):
    #     post = Post.objects.all()
    #     context['eve'] = post
    #     return super(categAdmin,self).render_change_form(request, context, **kwargs)

    def item_add(request, self, post_id):
        tree = post_id
        return self.add_view(request, extra_context={'tree': tree})

i am getting error item_add() missing 1 required positional argument: 'request'

Upvotes: 1

Views: 11460

Answers (2)

kumarD
kumarD

Reputation: 592

Always remember that methods are bound to objects and whenever you call methods, python implicitly passes the self argument(the object on which the method is being called) to the method call, In your example:

class CategAdmin:
      def item_add(self, request, post_id):
           pass

would be the signature format, note that the self object is the first parameter in the method signature. So when you do

categoryAdmin = CategAdmin()
categoryAdmin.item_add(request,123)
this is what will be called by the python interpreter CategAdmin.item_add(categoryAdmin,request,123)

One more feedback would be to improve your coding style, i.e follow some conventions like always start class names with Capital letter, give meaningful names to class and methods and variables. This makes your code more readable and via this debugging will be way faster.

Cheers!

Upvotes: 1

Max
Max

Reputation: 1844

You need to swap self and request parameters.

def item_add(self, request, post_id):
    tree = post_id
    ...

Upvotes: 1

Related Questions