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})
On Mon, Aug 24, 2020 at 7:57 PM hans alexander wrote:
> 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.
>
> On Mon, Aug 24, 2020 at 9:19 PM vipul shinde
> wrote:
>
>>
>> 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
>> viewsay 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
>>
>> --
>> 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+unsubscr...@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
>> <https://groups.google.com/d/msgid/django-users/CANE4xcuDmZCAaB4pzTqvH1d2eGf2b12%2BBPwN90e5_hmK%3DUBeQw%40mail.gmail.com?utm_medium=email_source=footer>
>> .
>>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CANV3w%3DYE%3DP6OW4cow0q%3D8uocbUSKoi259CJbRWrJYPo7A51oEw%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CANV3w%3DYE%3DP6OW4cow0q%3D8uocbUSKoi259CJbRWrJYPo7A51oEw%40mail.gmail.com?utm_medium=email_source=footer>
> .
>
--
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CANE4xcv3hx8pkaOh0E5xJg4_mOQjdnkGHt_4%3Dq61yZfMVodFyg%40mail.gmail.com.