Display information according to the logged in user, ListView class? - Django

0

models.py

class Torneo(models.Model):
    user = models.ForeignKey(User)
    descripcion = models.CharField(max_length=200)

  def __str__(self):
     return (self.descripcion)

views.py

 class Torneo_ListView(ListView):    
     template_name = 'torneos/torneo_listar.html'

    def get_queryset(self, *args, **kwargs):
        return Torneo.objects.filter(user=self.request.user)

I get this error, please help! I'm new using the framework. Thanks.

    
asked by jhon perez 31.03.2017 в 23:47
source

2 answers

1

The error that shows you indicates that the code ID is wrong. In the code that you post the indentation is wrong in both classes, I do not know if it is a result of copying the code or you have it that way originally. If you use a 4-space indentation that is recommended in PEP 8 it should be like this:

models.py

class Torneo(models.Model):
    user = models.ForeignKey(User)
    descripcion = models.CharField(max_length=200)

    def __str__(self):
        return (self.descripcion)

views.py

class Torneo_ListView(ListView):    
    template_name = 'torneos/torneo_listar.html'

    def get_queryset(self, *args, **kwargs):
        return Torneo.objects.filter(user=self.request.user)

Also watch that you are not mixing spaces and tabs to devise the code.

    
answered by 01.04.2017 / 00:57
source
0

Generally, a standard project is created with some "factory" configurations, including in the templates section, the django.template.context_processors.request that provides the globlal variable resquest .

Not part of your object , in this case, it is not part of the Torneo_ListView class, so you should not call it from self .

So things, this code should work.

 class Torneo_ListView(ListView):    
     template_name = 'torneos/torneo_listar.html'

    def get_queryset(self, *args, **kwargs):
        return Torneo.objects.filter(user=request.user)
  

Note

     

It is important that you make sure you have the corresponding processors. Verify in your configuration file settings.py that you have something similar to this:

  TEMPLATES = [
      {
          # algunas otras opciones 
          'OPTIONS': {
              # Otras opciones 
              'context_processors': [
                  # ... 
                  # muchos procesadores.... 
                  'django.template.context_processors.request',
                  # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                  # este debe estar presente
              ],
          },
      },
  ]
    
answered by 01.04.2017 в 00:04