0
I’m trying to develop a quiz with Django, in my logic I created a class in view.py, but now I don’t know how to reference the methods of this class in the url.py
py views.
from django.http import HttpResponse
from django.shortcuts import render
from .models import Disciplina, Topico, Questao, Teste
from .forms import SelecaoForm
import json
def index(request):
return render(request, 'freque_play/index.html')
def disciplinas(request):
"""Mostra a lista de todas as Disciplinas."""
disciplinas = Disciplina.objects.order_by('nome_disciplina')
context = {'disciplinas' : disciplinas}
return render(request,'freque_play/disciplinas.html', context)
def disciplina(request, disciplina_id):
"""Mostra a lista de topicos e subtopicos de uma disciplina."""
def novo_teste(request):
"""Mostra o formulário de seleçao de questões"""
form = SelecaoForm()
disciplina = Disciplina.objects.all()
context = {
'form': form,
'disciplina': disciplina,
}
return render(request, 'freque_play/novo_teste.html',context)
def get_topico(request, disciplina_id):
disciplina = Disciplina.objects.get(pk=disciplina_id)
topicos = Topico.objects.filter(disciplina=disciplina)
topicos_dict = {}
for topico in topicos:
topicos_dict[topico.id] = topico.nome_topico
return HttpResponse(json.dumps(topicos_dict), content_type="application/json")
def new_teste(request):
topico_id = request.POST.get("topico")
topico= Topico.objects.get(pk=topico_id)
questoes = Questao.objects.filter(topico=topico)
respostas = {}
teste = Teste(questoes, respostas)
quiz = QuizRun(request,teste)
class QuizRun(request, teste):
questao_atual = 0
def __init__(self, request, teste)
self.display_question(request, questao_atual)
def display_question(self, request, questao_seq):
questao = teste.questoes[questao_seq]
resposta = teste.respostas[questao.id]
if resposta = ""
comentar = False
mensagem = ""
elif resposta == questao.aCorreta:
comentar = True
mensagem = "você acertou alternativa correta " + questao.aCorreta
else
comentar = True
mensagem = "Resposta errada " + resposta ", alternativa correta " + questao.aCorreta
context = {
'questao': questao,
'comentar' : comentar,
'mensagem' : mensagem,
}
return render(request, 'freque_play/teste.html', context)
def next_question(self, request):
if self.questao_atual < len(teste.questoes):
self.questao_atual = questao_atual + 1
self.display_question(request, questao_atual)
def previous_question(self, request):
if self.questao_anterior > 0:
self.questao_atual = questao_atual - 1
self.display_question(request, questao_atual)
def responder(self, request):
alternativa = request.POST.get("options")
questao = teste.questoes[questao_atual]
if questao.responder(alternativa):
questao.status = 1
questao.salve()
teste.respostas[questao.id] = alternativa
self.display_question(request, questao_atual)
else:
questao.status = 0
questao.salve()
teste.respostas[questao.id] = alternativa
self.display_question(request, questao_atual)
py.
path('responder_teste', views.responder_teste, name='responder_teste'),
path('next_question', views.next_question, name='next_question'),
path('previous_question', views.responder, name='previous_question'),
(Obviously these urls didn’t work, it’s just an example) how would be the right way to make these urls? Or there is no way to do and there is something wrong in my logic?
Thank you I created a class to store the list of questions that is in the test object, which contains two lists one with the questions and the other with the answers given. I thought I’d save it in a class to give you access to all the methods. If not creating a class, where should I instantiate these objects so that all methods have access? Thank you
why did you create a class? What’s the point of using a class in this case?
– nosklo
Its logic is completely disconnected with the recommendations of Django and even python, this class would only make sense if you were in a package with generic functions, if you want to use classes in the views (and it is recommended), use the CBV that are a hand on the wheel for CRUD.
– Sidon
I created a class to store the list of questions that is in the test object, which contains two lists one with the questions and the other with the answers given. I thought I’d save it in a class to give you access to all the methods. If not creating a class, where should I instantiate these objects so that all methods have access? Thank you
– Renato Marques Dos Santos Guma
By the code you present you’re doing CRUD, right? So, as I said in the previous comment, create a class (CBV) for each view, then you get a listing with 3, 4 lines, a detail with 2 lines and so on. There are several examples on the internet, read this text, to begin with.
– Sidon