What am trying to do is have two different user locations. One is from my user model and the other is default from view. I want that anytime a user is authenticated, the location from the model shows but if not logged in the default location should be passed to the view.
Views.py
class Home(generic.TemplateView):
template_name = "Home.html"
def get_context_data(self, **kwargs):
context = super(Home, self).get_context_data(**kwargs)
if User.is_authenticated:
map_location = self.request.user.location_on_the_map
else:
map_location = Point(longitude, latitude, srid=4326)
context.update({
'job_listing': JobListing.objects.annotate(
distance=Distance("location_on_the_map", map_location)
).order_by("distance")[0:6]
})
return context
CodePudding user response:
Change this User.is_authenticated
to self.request.user.is_authenticated
CodePudding user response:
You need to modify the line User.is_authenticated to self.request.user.is_authenticated to ensure you are referencing the current user.
Alternatively you can add a request parameter on your function thus, you can use request.user.is_authenticated instead like so:
class Home(generic.TemplateView):
template_name = "Home.html"
def get_context_data(self, request, **kwargs):
context = super(Home, self).get_context_data(**kwargs)
if request.user.is_authenticated:
map_location = self.request.user.location_on_the_map
else:
map_location = Point(longitude, latitude, srid=4326)
context.update({
'job_listing': JobListing.objects.annotate(
distance=Distance("location_on_the_map", map_location)
).order_by("distance")[0:6]
})
return context