Think im getting there, but sorry for more questions.
If you dont mind answering these for me.
On the project_details
page i will have a button/link to take me to the questionnaire i want to complete.
the view for this link will need? to pass in the questionaire id
as well as the project_id
This will then load the questionaire page? But how do i set the formset
to only load the questions relating to that questionaire
?
this was my test view to see if i could get the form to load:
def formset_view(request):
context ={}
# creating a formset
fundamentals_questionnaire = modelformset_factory(Answer, fields=['answer'])
formset = AnswersForm()
# Add the formset to context dictionary
context['formset']= formset
return render(request, "pages/formset.html", context)
This is my view, url and link when trying to do it properly (doesn’t work):
<a class="dropdown-item" href="{% url 'questions' project.id questionnaire_id %}?next={{ request.path|urlencode }}">formset</a>
path("questions/<int:project_id>/<int:questionnaire_id>", view=add_fundamental_answers_view, name="questions"),
@login_required
def add_fundamental_answers_view(request,project_id,questionnaire_id):
project = get_object_or_404(Project, pk=project_id)
questionnaire = get_object_or_404(Questionnaire, pk=questionnaire_id)
next = request.POST.get('next', '/')
if request.method =='POST':
formset = AnswersForm()(request.POST)
if formset.is_valid():
answer = formset.save(commit=False)
answer.project_name = project
answer.save()
return HttpResponseRedirect(next)
else:
form = AnswersForm()
return render(request, 'pages/formset.html', {'project': project, "form": form})
using the test view i can load the html. Within the html template i am rendering the form with {{ formset }}
which is displaying the answer
field from the Answer
model.
How do i render the question?
At the moment within my html page, i can select the answer to the question, but this doesn’t update the database, but i think cause its not aware of the project_id
or questionnaire_id
or even what question the answer relates to?