Всплывающее окно с выбранными элементами из формы из python Django
Мне нужно показать выбранное значение с одной страницы на другую, то есть, пока я собираюсь обновить свой вопрос, мне нужно показать соответствующее поле на этой форме. Сейчас я выбираю значение с помощью выпадающего списка, вместо этого мне нужно, чтобы это значение отображалось во всплывающем окне.
Django form.py
class QuestionForm(forms.ModelForm):
#this will show dropdown __str__ method course model is shown on html so override it
#to_field_name this will fetch corresponding value user_id present in course model and return it
courseID=forms.ModelChoiceField(queryset=models.Course.objects.all(), to_field_name="id")
Это мой views.py
def update_question_view(request,pk):
question=QModel.Question.objects.get(id=pk)
#course=QModel.Question.objects.get(id=question.course_id)
questionForm=forms.QuestionForm(instance=question)
#CourseForm=forms.CourseForm(instance=course)
mydict={'questionForm':questionForm}
if request.method=='POST':
questionForm=forms.QuestionForm(request.POST,instance=question)
if questionForm.is_valid():
question=questionForm.save(commit=False)
course=models.Course.objects.get(id=request.POST.get('courseID'))
question.course=course
question.save()
else:
print("form is invalid")
return HttpResponseRedirect('/admin-view-question')
return render(request,'exam/update_question.html',context=mydict)
Это мой models.py
class Course(models.Model):
course_name = models.CharField(max_length=50)
question_number = models.PositiveIntegerField()
def __str__(self):
return self.course_name
class Question(models.Model):
course=models.ForeignKey(Course,on_delete=models.CASCADE)
question=models.CharField(max_length=600)
option1=models.CharField(max_length=200)
option2=models.CharField(max_length=200)
option3=models.CharField(max_length=200)
option4=models.CharField(max_length=200)
status= models.BooleanField(default=False)
cat=(('Option1','Option1'),('Option2','Option2'),('Option3','Option3'),('Option4','Option4'))
answer=models.CharField(max_length=200,choices=cat)
Это шаблон:
[![form method="POST" autocomplete="off" style="margin:100px;margin-top: 0px;">
{%csrf_token%}
<div class="form-group">
<label for="question">Skill Set</label>
{% render_field questionForm.courseID|attr:'required:true' class="form-control" %}
<br>
<label for="question">Question</label>
{% render_field questionForm.question|attr:'required:true' class="form-control" placeholder="Example: Which one of the following is not a phase of Prototyping Model?" %}
<br>
<label for="option1">Option 1</label>
{% render_field questionForm.option1|attr:'required:true' class="form-control" placeholder="Example: Quick Design" %}
<br>
<label for="option2">Option 2</label>
{% render_field questionForm.option2|attr:'required:true' class="form-control" placeholder="Example: Coding" %}
<br>
<label for="option3">Option 3</label>
{% render_field questionForm.option3|attr:'required:true' class="form-control" placeholder="Example: Prototype Refinement" %}
<br>
<label for="option4">Option 4</label>
{% render_field questionForm.option4|attr:'required:true' class="form-control" placeholder="Example: Engineer Product" %}
<br>
<label for="answer">Correct Answer</label>
{% render_field questionForm.answer|attr:'required:true' class="form-control" %}
<br>
<label for="answer">Question Verified{% render_field questionForm.status class="form-control" %}</label>
</div>][1]][1]