新的Django和我有一些问题,试图从我的模型在我的代码中显示时间戳。我有两个Django类代表两个不同的列表:
models.py
class Playinglist(models.Model):
title = models.CharField(max_length=200)
user = models.CharField(max_length=64)
game_id = models.IntegerField()
started_on = models.DateTimeField(auto_now=True)
class Playedlist(models.Model):
title = models.CharField(max_length=200)
user = models.CharField(max_length=64)
game_id = models.IntegerField()
finished_on = models.DateTimeField(auto_now=True)
当我创建一个新模型时,我理解models.DateTimeField(auto_now=True)
应该在模型中创建一个时间戳,用于该模型最后保存的时间。
我用来向这些模型添加数据的函数如下所示:
views.py
def add_to_playinglist(request, game_id):
obj = Playinglist.objects.filter(game_id=game_id, user=request.user.username)
if obj:
obj.delete()
game = Game.objects.get(id=game_id)
playing_game = Playinglist.objects.filter(game_id=game_id, user=request.user.username)
return redirect("profile")
else:
obj = Playinglist()
obj.user = request.user.username
obj.game_id = game_id
obj.save()
game = Game.objects.get(id=game_id)
playing_game = Playinglist.objects.filter(game_id=game_id, user=request.user.username)
return redirect("profile")
@login_required(login_url='/login')
def add_to_playedlist(request, game_id):
obj = Playedlist.objects.filter(game_id=game_id, user=request.user.username)
if obj:
obj.delete()
game = Playinglist.objects.get(game_id=game_id)
played_game = Playedlist.objects.filter(game_id=game_id, user=request.user.username)
return redirect("profile")
else:
obj = Playedlist()
obj.user = request.user.username
obj.game_id = game_id
obj.save()
game = Playinglist.objects.get(game_id=game_id)
game.delete()
played_game = Playedlist.objects.filter(game_id=game_id, user=request.user.username)
return redirect("profile")
当我尝试显示数据时,我使用这些函数:
views.py
playinglist = Playinglist.objects.filter(user=request.user.username)
playinglist_items = []
playing = playinglist.count()
present_in_playinglist = False
if playinglist:
for item in playinglist:
try:
game = Game.objects.get(id=item.game_id)
playinglist_items.append(game)
present_in_playinglist = True
except:
present_in_playinglist = False
playedlist = Playedlist.objects.filter(user=request.user.username)
playedlist_items = []
finished = playedlist.count()
present_in_playedlist = False
if playedlist:
for item in playedlist:
try:
game = Game.objects.get(id=item.game_id)
playedlist_items.append(game)
present_in_playedlist = True
except:
present_in_playedlist = False
我有麻烦确定如何以及在哪里添加代码来显示这些时间戳。对于我的playinglist
代码块,我尝试过:
playinglist = Playinglist.objects.filter(user=request.user.username)
playinglist_items = []
playing = playinglist.count()
present_in_playinglist = False
if playinglist:
for item in playinglist:
try:
game = Game.objects.get(id=item.game_id)
playinglist_items.append(game)
present_in_playinglist = True
started_on = Playinglist.objects.get(started_on)
except:
present_in_playinglist = False
为了显示这个,我使用了一个html页面来渲染它:
profile.html
{% if present_in_playinglist %}
{% for game in playinglist_items %}
<p>Started on: {{ started_on }}</p>
{% endfor %}
{% else %}
<h6>No games currently playing.</h6>
{% endif %}
然而,当我这样做,它似乎覆盖我的playinglist
,不返回任何东西。是我写错了,还是我把这行写错了?
编辑
我尝试了其他方法,这似乎得到了时间戳:
playinglist = Playinglist.objects.filter(user=request.user.username)
playinglist_items = []
playing = 0
present_in_playinglist = False
if playinglist:
for item in playinglist:
try:
game = Game.objects.get(id=item.game_id)
playinglist_items.append(game)
present_in_playinglist = True
playing += 1
playinglist = Playinglist.objects.get(id=item.game_id)
except:
present_in_playinglist = False
playedlist = Playedlist.objects.filter(user=request.user.username)
playedlist_items = []
finished = 0
present_in_playedlist = False
if playedlist:
for item in playedlist:
try:
game = Game.objects.get(id=item.game_id)
playedlist_items.append(game)
present_in_playedlist = True
finished += 1
playedlist = Playedlist.objects.get(game_id=item.game_id)
except:
present_in_playedlist = False
context = {
"playinglist": playinglist,
"playedlist": playedlist,
"present_in_playlist": present_in_playlist,
"playlist_items": playlist_items,
"saved": saved,
"present_in_playinglist": present_in_playinglist,
"playinglist_items": playinglist_items,
"playing": playing,
"present_in_playedlist": present_in_playedlist,
"playedlist_items": playedlist_items,
"finished": finished
}
return render(request, "capstone/profile.html", context)
和我的html渲染现在看起来像这样:<p>Started on: {{ playinglist.started_on }}</p>
然而,在这个解决方案中,每当我将一个项目从一个模型移动到下一个模型时,它将更新该列表中每个项目的时间戳。我好像被困在某个循环里了?
您没有在循环中正确存储started_on
值,并且将变量传递给上下文非常重要,以便它可以在模板中使用。
这些→
用下面的代码更新views.py
文件:
if playinglist:
for item in playinglist:
try:
game = Game.objects.get(id=item.game_id)
playinglist_items.append({'game': game, 'started_on': item.started_on})
present_in_playinglist = True
except:
present_in_playinglist = False
然后更新模板profile.html
:
{% if present_in_playinglist %}
{% for game in playinglist_items %}
<p>Started on: {{ game.started_on }}</p>
{% endfor %}
{% else %}
<h6>No games currently playing.</h6>
{% endif %}
这应该可以解决您遇到的问题。如果有任何问题,请在评论区提问。