简体   繁体   English

在Django中将对象分配给特定用户?

[英]Assign objects to specific user in django?

Trying to make my matches.html page show user specific timetable dat however when i log into another user the previous users data is still there. 尝试使我的matches.html页面显示特定于用户的时间表数据,但是当我登录另一个用户时,以前的用户数据仍然存在。 Is there a way i can make the timetable data assigned the user logged in. 有没有一种方法可以使时间表数据分配给用户登录。

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

Thats my models.py, this is views 多数民众赞成在我的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')

and here is my matches page where the time table data is viewed 这是我的比赛页面,用于查看时间表数据

<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>

You should link each TimeTableSetup object to a User in your database using a ForeignKey, then filter within your matches view to ensure that the timetable for the given user is shown: 您应该使用ForeignKey将每个TimeTableSetup对象链接到数据库中的User,然后在Matchs视图中进行过滤以确保显示给定用户的时间表:

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)
    user = models.ForeignKey(<POINT TO YOUR USER MODEL HERE>)


    def __str__(self):
        return self.module

def matches(request, user):

    # Get the user whose entries you want to show here
    # E.g., user = User.objects.get(id=3)
    user = <GET THE USER HERE>

    # Filter entrys to only those assigned to a certain user
    entrys = timetablesetup.objects.filter(user=user)

    context = {
        'entrys': entrys
    }
    return render(request, 'timetableMatch/matches.html', context)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM