【发布时间】:2019-02-21 12:56:22
【问题描述】:
试图让我的matches.html 页面显示用户特定的时间表数据,但是当我登录另一个用户时,以前的用户数据仍然存在。 有没有办法让我为登录的用户分配时间表数据。
class timetablesetup(models.Model):
module = models.CharField(max_length=150)
day = models.CharField(max_length=100)
time = models.IntegerField()
location = models.CharField(max_length=100)
def __str__(self):
return self.module
这是我的models.py,这是视图
def matches(request, user):
entrys = timetablesetup.objects.all()
context = {
'entrys': entrys
}
return render(request, 'timetableMatch/matches.html', context)
def add_entry(request):
return render(request, 'timetableMatch/add_entry.html')
def delete(request, id):
entrys = timetablesetup.objects.get(pk=id)
entrys.delete()
return redirect('/timetableMatch')
def edit(request, id):
entrys = timetablesetup.objects.get(pk=id)
context = {
'entrys': entrys
}
return render(request, 'timetableMatch/edit.html', context)
def update(request, id):
entrys = timetablesetup.objects.get(pk=id)
entrys.module = request.GET['module']
entrys.day = request.GET['day']
entrys.time = request.GET['time']
entrys.location = request.GET['location']
try:
entrys.save()
except Exception:
return redirect('/errorpage')
else:
return redirect('/timetableMatch')
这是我查看时间表数据的比赛页面
<div class="container"><br>
<h2>TimetableMatch</h2>
<hr>
<table class="table table-dark">
<thead>
<tr>
<th>Modules</th>
<th>Day</th>
<th>Time</th>
<th>Location</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for entry in entrys %}
<tr>
<td>{{ entry.module }}</td>
<td>{{ entry.day }}</td>
<td>{{ entry.time }}</td>
<td>{{ entry.location }}</td>
<td><a class="btn btn-info" href="../edit/{{ entry.id }}">Edit</a>
<a class="btn btn-danger" href="../delete/{{ entry.id }}">Delete</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
【问题讨论】:
-
您应该在用户模型和时间表之间添加一个外键链接。在 Django 文档中有很多很好的信息。
标签: python django object project assign