[Python] 'method' object is not subscriptable error appears when using get function

Discussão em 'Python' iniciado por Stack, Junho 26, 2025.

  1. Stack

    Stack Membro Participativo

    Django rises an error of ('method' object is not subscriptable) when trying to access username attribute from class User . imports :

    from django.shortcuts import render
    from .models import Post
    from django.contrib.auth.models import User
    from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
    from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
    from django.shortcuts import get_object_or_404


    the function that cause the error :

    class UserPostListView(PostListView):
    model = Post
    fields = ['title', 'content']
    context_object_name = 'posts'
    paginate_by = 2
    def get_queryset(self):
    user = get_object_or_404(User, username= self.kwargs.get('username'))
    return Post.objects.filter(author=user).order_by['-date']


    I tried using square brackets instead of parentheses, but it raised a similar error .

    Continue reading...

Compartilhe esta Página