2 回答
TA贡献1757条经验 获得超7个赞
在您的视图中,您正在使用关键字 user 过滤 Event 对象,但您的事件模型没有名为 user 的字段。如果您希望您的事件与用户相关联,您可以将外键添加到您的事件模型中:
class Event(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT)
title = models.CharField(max_length=200)
description = models.TextField()
start_time = models.DateTimeField()
end_time = models.DateTimeField()
@property
def get_html_url(self):
url = reverse('cal:event_edit', args=(self.id,))
return f'<a href="{url}"> {self.title} </a>'
或者,在您看来,您可以简单地通过另一个关键字过滤您的 Event 对象,这些关键字是您的错误为您提供的选择,以及您在模型中指定的字段。所以在你看来你可以这样做:
def login_user(request):
if request.method == "POST":
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
events = Event.objects.filter(title='Title you want to filter by') # This line seems to be the source of your error. You just need to filter by a keyword that exists in your model.
login(request, user)
return render(request, 'cal/calendar.html', {'calendar': calendar})
else:
return render(request, 'cal/login.html', {'error_message': 'Your account has been disabled'})
else:
return render(request, 'cal/login.html', {'error_message': 'Invalid login'})
return render(request, 'cal/login.html')
TA贡献1777条经验 获得超10个赞
在您的 models.py 中,您错过了为您的用户表添加关系。添加用户并执行
python manage.py makemigrations myapp
python manage.py 迁移 myapp
from django.contrib.auth import get_user_model
class Event(models.Model):
user = models.ForeignKey(get_user_model(), null=True)
title = models.CharField(max_length=200)
description = models.TextField()
start_time = models.DateTimeField()
end_time = models.DateTimeField()
@property
def get_html_url(self):
url = reverse('cal:event_edit', args=(self.id,))
return f'<a href="{url}"> {self.title} </a>'
添加回答
举报