How to pass a set of objects in between two views?

92 views
Skip to first unread message

vipul shinde

unread,
Aug 24, 2020, 10:19:46 AM8/24/20
to django...@googlegroups.com

I'm building a quiz app in which I'm storing questions in the database by creating a model class. I am retrieving a random question set for each user from the database and then rendering them on an HTML page. The problem is after logging a user in, a random set of questions appears but that random set is lost after refreshing the page.
How do I solve this
One thing that I thought was retrieving the object set in another view....say after logging a user in and passing it as a dictionary to another view.
But I can't find the syntax or any function (if it exists). Please help.
I'm using django 3.1 and MySQL as my database

hans alexander

unread,
Aug 24, 2020, 10:28:21 AM8/24/20
to django...@googlegroups.com
Can you share the views.py that you wrote?
Actually If the page for random set of questions is same for User NOT Logged In and User Logged In, the data you called from database will still showing up.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANE4xcuDmZCAaB4pzTqvH1d2eGf2b12%2BBPwN90e5_hmK%3DUBeQw%40mail.gmail.com.

vipul shinde

unread,
Aug 24, 2020, 10:32:17 AM8/24/20
to django...@googlegroups.com
from django.shortcuts import render, redirect
from .models import *
from .forms import UserForm
from django.contrib.auth.forms import AuthenticationForm
import random
from django.contrib.auth import login, logout, authenticate

# Create your views here.
def home(request):
    return render(request, 'testapp/home.html')

def loginuser(request):
    #form = UserForm()
    if request.method == 'GET':
        form = AuthenticationForm()
        return render(request, 'testapp/login.html', {'form':form})
    else:
        user = authenticate(request, username=request.POST['username'], password=request.POST['password'])
        if user is None:
            return render(request, 'testapp/login.html', {'form':AuthenticationForm(), 'error':'Username or password incorrect'})
        else:
            login(request, user)
            return redirect('paper')

def paper(request):
    #objects = Questions.objects.all()
    """count = Questions.objects.all().count()
    slice = random.random() * (count-5)
    objects = Questions.objects.all()[slice:slice+5]"""
    #objects = {{ objects }}
    objects = Questions.objects.all().order_by('?')[:5]
    return render(request, 'testapp/paper.html', {'objects':objects})

Kunal Solanke

unread,
Aug 24, 2020, 11:55:02 AM8/24/20
to django...@googlegroups.com
Two things come to my mind
1.Either create a questionlist model with for each user.
2.Bind questionlist to session.

Reply all
Reply to author
Forward
0 new messages