【发布时间】:2023-08-14 09:12:01
【问题描述】:
我有下面的代码if self.request.user.is_authenticated() and self.request.user. userlocation 我不明白为什么我得到这个用户没有用户定位错误。如果不满足他的条件,我有一个 if 语句,它不应该只是向下显示上下文
class Homepage(TemplateView):
template_name = 'home.html'
def get_context_data(self, **kwargs):
context = super(Homepage, self).get_context_data(**kwargs)
context['event_list'] = Event.objects.all()
if self.request.user.is_authenticated() and self.request.user.userlocation:
print("The code reached here ")
return context
下面是models.py
class UserLocation(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
lat = models.FloatField(blank=True, null=True)
lon = models.FloatField(blank=True, null=True)
point = models.PointField(srid=4326, default='SRID=4326;POINT(0.0 0.0)')
objects = models.GeoManager()
【问题讨论】:
-
这是来自
UserLocation的OneToOneField吗? -
@WillemVanOnsem 是的
OneToOneField我已经在问题中添加了模型 -
@WillemVanOnsem 你能在上面的问题中检查我对你的代码的实现吗我做错了什么我仍然遇到同样的错误
-
你
importObjectDoesNotExists出错了吗? -
@WillemVanOnsem 是的
from django.core.exceptions import ObjectDoesNotExist我正在使用 pycharm 专业版,它向我显示了这些错误
标签: django python-3.x django-templates django-views