Reputation: 43
from django.http import HttpResponse
from .models import Destination
def index(request):
boards = Destination.objects.all()
boards_names = list()
for Destination in boards:
boards_names.append(Destination.destinationtext)
response_html = '<br>'.join(boards_names)
return HttpResponse(response_html)
I have written this code following just for practice of django framework but I am getting the following errors through pylint :
E1101:Class 'Destination' has no 'objects' member
E0601:Using variable 'Destination' before assignment
Upvotes: 4
Views: 12764
Reputation: 1475
You have two different issues, and not just one as you say:
E1101:Class 'Destination' has no 'objects' member
: Is a warning
that occurs because pylint
doesn't know about our special Django variables. A pylint plugin like pylint-django
might do the trick.
E0601:Using variable 'Destination' before assignment
: In the for loop in your code you defined a variable called Destination
. This is not only bad practice because python variables need to be in lowercase_underscore
but it overrides the Destination
class, and that's what is causing this error. You probably wanted to do something like this:
for d in boards:
# Or:
for destination in boards:
Upvotes: 1
Reputation: 477616
You wrote in your view:
for Destination in boards:
# ...
This means that Python sees Destination
as a local variable, a local variable that you use before it is assigned.
You can rename the variable in the loop to solve the problem, but actually you can make it more elegant and faster here by using .values_list(..)
:
from django.http import HttpResponse
from .models import Destination
def index(request):
response_html = '<br>'.join(
Destination.objects.values_list('destinationtext', flat=True)
)
return HttpResponse(response_html)
Nevertheless, I'm still not convinced that this solves the matter, since the destinationtext
could contain HTML, which then will mix up in the response. Usually it is better to use templates.
Upvotes: 1