这是我在静态文件夹中的js文件:
let questions = [
{
question :"my question",
choiceA : "Correct",
choiceB : "Wrong",
choiceC : "Wrong",
correct : "B"
}
这是管理员.py
from django.contrib import admin
from .models import Question
admin.site.register(Question)
这是型号.py
class Question(models.Model):
name=models.CharField(max_length=300)
views.py
def quiz(request):
question=Question.objects.all()
return render(request,"index.html",{"question":question})
这个问题是应该在js文件中写什么来代替";我的问题"?
在index.html中
<script>
{% for question in questions %}
let question = {}
question["name"] = problem.name
questions.push(question)
{% endfor %}
</script>
在views.py中,为了增加可读性,更改
{"question": question} to {"questions": question}
注意";s";在";问题";。这提高了for循环的可读性