Ritankar Bhattacharjee
Ritankar Bhattacharjee

Reputation: 766

pk is passed but data not displayed

I have a model that contains some judgments along with their title. Now i want to display the judgement when the related title is clicked. i have written the following code:

models.py

class Laws(models.Model):
    date= models.DateField(default=timezone.now)
    title= models.CharField(max_length=225, help_text="judgement title")
    judgements= RichTextField(blank=True, null= True)
    law_type= models.CharField(max_length=40, choices= type_of_law)
    law_category= models.CharField(max_length=60, choices= category)

views.py

class DocView(ListView):
    model= Laws
    template_name = 'doc.html'

class Doc1DetailView(DetailView):
    model= Laws
    template_name = 'doc1.html'

urls.py

urlpatterns=[
    path('doc', DocView.as_view(), name='doc' ),
    path('doc1/<int:pk>', Doc1DetailView.as_view(), name='doc1' ),
]

and the html files are doc.html

{% for data in object_list %}
                    
    <div class="tab">
        <a href="{% url 'doc1' data.pk %}">{{data.title}}</a><br/> 
    </div>
       
                    
{% endfor %}

doc1.html

<p style="color: black;">{{data.judgements}}</p>

I refered to a video in youtube and wrote this code his was working and mine not. i have chamged nothing from that code still the data doesn't show up in doc1.html. No filed is empty here.

please rectify my code and tell me what i am doing wrong here.

Upvotes: 1

Views: 40

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477210

The Laws object is not passed as data, but as object (and also as laws), so you render this with:

<p style="color: black;">{{ object.judgements }}</p>

Note: normally a Django model is given a singular name, so Law instead of Laws.

Upvotes: 1

Related Questions