[ad_1]
I am working on a form builder similar to Google Forms but I came across an error when building the ‘Edit response’ function.
The error stated that the “Answer matching query does not exist.”
This is my HTML code for the edit response page:
28 {% endif %}
29 {% for question in form.questions.all %}
30 <div class="margin-top-bottom box question-box">
31 {% if form.is_quiz %}
32 <h1 class="question-title txtClr">{{question.question}} {% if question.required %}<span class="require-star">*</span>{% endif %}</h1>
33 {% else %}
34 <h1 class="question-title txtClr" oncopy = "return false">{{question.question}} {% if question.required %}<span class="require-star">*</span>{% endif %}</h1>
35 {% endif %}
36 {% if question.question_type == "short" %}
37 <input type="text" name="{{question.id}}" class="short-answer" placeholder="Your answer" {% if question.required %} required {% endif %}
38 value="{{response|get_response:question.pk}}">
39 {% elif question.question_type == "paragraph" %}
40 <textarea name="{{question.id}}" placeholder="Your answer" class="long-answer textarea-adjust"
41 {% if question.required %} required {% endif %}>{{response|get_response:question.pk}}</textarea>
42 {% elif question.question_type == "multiple choice" %}
43 {% for choice in question.choices.all %}
44 <div class="multiple-choice">
45 {% if response|get_response:question.pk|to_int == choice.pk|to_int %}
46 <input type="radio" name="{{question.id}}" id="{{choice.id}}" {% if question.required %} required {% endif %} value="{{choice.id}}" checked>
47 {% else %}
48 <input type="radio" name="{{question.id}}" id="{{choice.id}}" {% if question.required %} required {% endif %} value="{{choice.id}}">
The error seems to be coming from line 38, and this string “{{response|get_response:question.pk}}” was in red.
views.py:
def edit_response(request, code, response_code):
formInfo = Form.objects.filter(code = code)
#Checking if form exists
if formInfo.count() == 0:
return HttpResponseRedirect(reverse('404'))
else: formInfo = formInfo[0]
response = Responses.objects.filter(response_code = response_code, response_to = formInfo)
if response.count() == 0:
return HttpResponseRedirect(reverse('404'))
else: response = response[0]
if formInfo.authenticated_responder:
if not request.user.is_authenticated:
return HttpResponseRedirect(reverse("login"))
if response.responder != request.user:
return HttpResponseRedirect(reverse('403'))
if request.method == "POST":
if formInfo.authenticated_responder and not response.responder:
response.responder = request.user
response.save()
if formInfo.collect_email:
response.responder_email = request.POST["email-address"]
response.save()
#Deleting all existing answers
for i in response.response.all():
i.delete()
for i in request.POST:
#Excluding csrf token and email address
if i == "csrfmiddlewaretoken" or i == "email-address":
continue
question = formInfo.questions.get(id = i)
for j in request.POST.getlist(i):
answer = Answer(answer=j, answer_to = question)
answer.save()
response.response.add(answer)
response.save()
if formInfo.is_quiz:
return HttpResponseRedirect(reverse("response", args = [formInfo.code, response.response_code]))
else:
return render(request, "index/form_response.html", {
"form": formInfo,
"code": response.response_code
})
return render(request, "index/edit_response.html", {
"form": formInfo,
"response": response
})
urls.py
path('form/<str:code>/response/<str:response_code>', views.response, name="response"),
path('form/<str:code>/response/<str:response_code>/edit', views.edit_response, name="edit_response"),
Does anyone have a solution to this? Thanks
[ad_2]