a variable is not defined

19 views
Skip to first unread message

sotiris moustogiannis

unread,
Oct 17, 2019, 10:13:20 PM10/17/19
to Django users
I have an error: name 'result' is not defined


def search(request):

   
def get_queryset(self):
        query
= self.request.GET.get('q')
        result
= Shops.objects.filter(Q(city=query))

       
return result
   
    context
={
       
'shops' : result,
   
}

   
return render(request, 'booking/search.html',context)





wd

unread,
Oct 17, 2019, 10:24:49 PM10/17/19
to django...@googlegroups.com
Dude, you need to indent at the line include 'context' ... it's a beginners  problem...

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/543debc7-d517-4a7a-a9b8-f3fc7c7cc60d%40googlegroups.com.

wd

unread,
Oct 17, 2019, 10:25:38 PM10/17/19
to django...@googlegroups.com
oh ,sorry my mistake. But I think the error is pretty clear ...

Jorge Gimeno

unread,
Oct 17, 2019, 10:49:48 PM10/17/19
to django...@googlegroups.com
wd,

The problem here is that when you're creating the context, the name 'result' is not defined. When the get_queryset() method returns, the result name goes out of scope.  My guess that's why you're getting a NameError.

If that's not it, I would suggest cutting and pasting the traceback and showing us the actual code (with line numbers if you have them). It will help us diagnose the problem.

-Jorge

sotiris moustogiannis

unread,
Oct 17, 2019, 11:03:18 PM10/17/19
to Django users
I read that you can't use the def get_queryset into a function but only in a generic class view, so maybe the def get_queryset function does not run. I want to  make my own custom function and i want to find a way to get self parameter in my function. Thanks a lot.


On Friday, October 18, 2019 at 5:49:48 AM UTC+3, jlgimeno71 wrote:
wd,

The problem here is that when you're creating the context, the name 'result' is not defined. When the get_queryset() method returns, the result name goes out of scope.  My guess that's why you're getting a NameError.

If that's not it, I would suggest cutting and pasting the traceback and showing us the actual code (with line numbers if you have them). It will help us diagnose the problem.

-Jorge

On Thu, Oct 17, 2019 at 7:25 PM wd <w...@wdicc.com> wrote:
oh ,sorry my mistake. But I think the error is pretty clear ...

On Fri, Oct 18, 2019 at 10:23 AM wd <w...@wdicc.com> wrote:
Dude, you need to indent at the line include 'context' ... it's a beginners  problem...

On Fri, Oct 18, 2019 at 10:14 AM sotiris moustogiannis <soto...@outlook.com> wrote:
I have an error: name 'result' is not defined


def search(request):

   
def get_queryset(self):
        query
= self.request.GET.get('q')
        result
= Shops.objects.filter(Q(city=query))

       
return result
   
    context
={
       
'shops' : result,
   
}

   
return render(request, 'booking/search.html',context)





--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

sotiris moustogiannis

unread,
Oct 17, 2019, 11:14:03 PM10/17/19
to Django users
I want to make custom function because i want to take one result from 2 different models (inner join), and i dont find a way to use 2 models in my listview class (generic)

Jorge Gimeno

unread,
Oct 17, 2019, 11:59:05 PM10/17/19
to django...@googlegroups.com

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/dcd65a19-628b-4ac9-9f60-d8d4b726d945%40googlegroups.com.

sotiris moustogiannis

unread,
Oct 18, 2019, 2:48:47 PM10/18/19
to Django users
thanks for your help!!

Joalbert Palacios

unread,
Oct 18, 2019, 5:23:14 PM10/18/19
to django...@googlegroups.com
Not sure, but I believe that is because scope, given that result variable is defined in the inner method and not defined in search function.

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c602853d-b5c7-4105-b77c-95ed37ac67ea%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages