CodeMonkey
CodeMonkey

Reputation: 1

How do you pass model data to a template using Django?

I am trying to pass data from a Django model into an HTML template using a class based view.

urls.py

from django.urls import path
from app import views

urlpatterns = [
    path('review/', views.ReviewRecord.as_view(template_name='review.html'), name='review')
]

models.py

from django.db import models

class MyModel(models.model):
    RegNumber = models.TextField(primary_key=True)
    Description = models.TextField(null=True)

views.py

from app.models import MyModel
from django.views.generic import TemplateView

class ReviewRecord(TemplateView)
    template_name = 'review.html'
    model = myModel

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['description'] = self.model.description
        return context

html

<textarea readonly>{{ description }}</textarea>

The above code inserts the following into the html textarea:

<django.db.models.query_utils.DeferredAttribute object at 0x0000024C449B9F88>

I need to display the stored field data from the model, not the object data as stated above.

I am trying to create a queryset based on a field in the model, for example, for a particular RegNumber. Eventually I would like to retrieve several records and be able to increment through them, however I am currently just trying to get one to work. I have also tried using a DetailView using the primary key in the url, however I keep getting errors, therefore the provided example code is from what appears to be my closest attempt to my goal.

Upvotes: 0

Views: 3308

Answers (2)

minglyu
minglyu

Reputation: 3327

You need to pass model instances/queryset instead of the model class as mentioned in the comments

class ReviewRecord(TemplateView):
    template_name = 'review.html'
    model = Mymodel
    pk = 1

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # specfic instance description
        context['Description'] = Mymodel.objects.get(pk=self.pk).Description
        # all instances' descriptions
        context['all_descriptions'] = Mymodel.objects.values_list('Description', flat=True)
        # pass other variables
        context['other_var'] = 'other_var'
        return context

Upvotes: 2

Tanishq Vyas
Tanishq Vyas

Reputation: 1689

A very easy way to do what you need is this.

views.py


from django.shortcuts import render
from .models import MyModel

def review_view(request, *args, **kwargs):

    # Getting all the stuff from database
    query_results = MyModel.objects.all();

    # Creating a dictionary to pass as an argument
    context = { 'query_results' : query_results }

    # Returning the rendered html
    return render(request, "review.html", context)

models.py : Here, make sure you write models.Model instead of models.model

from django.db import models

class MyModel(models.Model):
    RegNumber = models.TextField(primary_key=True)
    Description = models.TextField(null=True)

urls.py

from django.urls import path
from app.views import review_view

urlpatterns = [
    path('review/', review_view , name='review')
]

The file review.html. Make sure you specify the correct DIR for templates in the settings.py to avoid template not found error

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>

<body>

        {% for item in query_results %}
           <h1>{{ item.RegNumber }}</h1>
           <p>{{ item.Description }}</p>
        {% endfor %}

</body>
</html>

Upvotes: 2

Related Questions