In django, how do I pass in context to a view for a specific entry in my database?

Question:

I’m quite new to django and python so please bear with me. I’m trying to create a view inside my django project that will display one entry inside my database. I have a specific template for how I want the data in the entry to be displayed.

I have an app inside my project called card_catalog. Inside this app I have a function based view. Here is the code from the views.py file.

def flashcard_view(request):
    sentence = Flashcard.objects.values_list('sentence',flat=True)
    word_dict_form = Flashcard.objects.only('word_dict_form')
    ipa = Flashcard.objects.only('ipa')
    pos = Flashcard.objects.only('pos')
    word_trans = Flashcard.objects.only('word_trans')
    sent_trans = Flashcard.objects.only('sent_trans')

    context = {
        'sentence':sentence,
        'word_dict_form':word_dict_form,
        'ipa':ipa,
        'pos':pos,
        'word_trans':word_trans,
        'sent_trans':sent_trans
    }
    return render(request,'card_catalog/flashcard_view.html',context=context)

Here is the code for the flashcard model in the models.py file:

class Flashcard(models.Model):
    #Main fields
    sentence = models.CharField(max_length=250,null=True,blank=True)
    word_dict_form = models.CharField(max_length=50,null=True,blank=True)
    word_inflected_form = models.CharField(max_length=50,null=True,blank=True)
    ipa = models.CharField(max_length=50,null=True,blank=True)
    pos = models.CharField(max_length=50,null=True,blank=True)
    word_trans = models.CharField(max_length=50,null=True,blank=True)
    sent_trans = models.CharField(max_length=250,null=True,blank=True)
    # sound_word =
    # sound_sentence =
    # notes_visible
    # notes_not_visible

    # Card Activation fields
    activate_nw_card = models.BooleanField(default=True)
    activate_pic_word_card = models.BooleanField(default=True)
    activate_word_sent_card = models.BooleanField(default=True)
    activate_lr_card = models.BooleanField(default=False)
    activate_inflected_form_card = models.BooleanField(default=False)
    activate_pron_card = models.BooleanField(default=False)
    activate_spell_card = models.BooleanField(default=False)
    activate_trans_word_card = models.BooleanField(default=False)
    activate_sent_trans_card = models.BooleanField(default=False)

    # Image Links
    image_1_url = models.URLField(max_length=500,null=True,blank=True)
    image_1_overlay = models.URLField(max_length=500, null=True, blank=True)
    image_2_url = models.URLField(max_length=500, null=True, blank=True)
    image_2_overlay = models.URLField(max_length=500, null=True, blank=True)
    image_3_url = models.URLField(max_length=500, null=True, blank=True)
    image_3_overlay = models.URLField(max_length=500, null=True, blank=True)
    image_4_url = models.URLField(max_length=500, null=True, blank=True)
    image_4_overlay = models.URLField(max_length=500, null=True, blank=True)
    image_5_url = models.URLField(max_length=500, null=True, blank=True)
    image_5_overlay = models.URLField(max_length=500, null=True, blank=True)

    class Meta:
        ordering = ['word_dict_form']

    # def get_absolute_url(self):
    #     return reverse('card_catalog/flashcard_detail',kwargs={"pk": self.pk})

    def __str__(self):
        return f'Sentence: {self.sentence}nTarget Word: {self.word_dict_form}'

As you can see I’m a little confused about what to do with the code at the end. I believe I need to use get_absolute_url to access each individual entry. If I only have on entry in my database, my template is showing this:

enter image description here

The code for this template goes something like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
     {% load static %}
    <link rel="stylesheet" href="{% static 'am_flashcard_template.css' %}">
</head>
<body>
<h1>Flashcard Display</h1>
{{ flashcard }}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" referrerpolicy="no-referrer"></script>

<div class="card">

    <div class='top'>
    <span></span>
    <span class='red-card' >NW</span>
    </div>

    <div id='sentence-example'>{{sentence}}</div>
    <div class='dictionary-form'>{{word_dict_form}}</div>
    <div class='brown mid-text'>{{ipa}}</div>

enter image description here

Asked By: Vinny

||

Answers:

Adjust your view to be more like:

from django.shortcuts import render, get_object_or_404

def flashcard_view(request, id):
    flashcard = get_object_or_404(Flashcard, id=id)

    context = {
        'flashcard':flashcard,
    }
    return render(request,'card_catalog/flashcard_view.html',context=context)

Then for each property/field you want to show from the flashcard, in your template use something like:

<div id='sentence-example'>{{flashcard.sentence}}</div> 

in your urls.py you’ll need an entry in your url patterns similar to:

path('flashcard-detail/<int:id>', flashcard_view, name='flash_card_detail'),
Answered By: AMG

Beatiful! I followed the instructions of AMG and the flashcard is looking much nicer. I had to add a few import statements at the top but other than that this solution worked as is. Here’s a screenshot of the new flashcard display:
new flashcard display

I still need to fix those images so I will post again for that.

Answered By: Vinny
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.