简体   繁体   中英

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

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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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