Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send object from detail view to another view in Django?

I have a detail view that uses a Quiz object to display data stored in that object, like title and author. I want to have a button that links to a new page that displays different data from the same object. I don't know how to pass this data/object.

I can render the view and pass it the context of a specific quiz using an id but I want the id to change to be the id of the object from the initial page.

#assessement view
def assessment(request):
    context = {
        'quiz':Quiz.objects.get(id=1),
    }
    return render(request, 'quiz_app/assessment.html', context)

#detailview template for quiz
{% extends "quiz_app/base.html" %}
{% block content %}
        <article class="quiz-detail">
            <h1>{{ object.title }}</h1>
            <h2>{{ object.question_amount }} Questions</h2>
            <a class="btn" href="{% url 'quiz-assessment' %}">Start Quiz</a>
        </article>
{% endblock content %}

#assessment template
{% extends "quiz_app/base.html" %}
{% block content %}
    <h2>Assessment</h2>
    <h2>Title is {{ quiz.title }}</h2>
{% endblock content %}
like image 883
Synnipoe Avatar asked Jan 29 '26 19:01

Synnipoe


1 Answers

Then you should make another view for url quiz-assessment and pass the quiz pk as you did above in your assessment view.

 def quiz_assessment(request,pk):
         quiz = Quiz.objects.get (pk=pk)
         return render (request,'assessment_template', {'quiz':quiz}

And in your url,pass the quiz id like this:

         path ('<int:pk>/quiz/assessment /',views.quiz_assessment,name='quiz_assessment')

And in your template you can give url like this:

        < a class="btn" href="{% url 'quiz_assessment' object.pk %}>
like image 194
arjun Avatar answered Jan 31 '26 10:01

arjun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!