Skip to content Skip to sidebar Skip to footer

How To Display One Field From A Model In Django Template

I've inherited a Django project to maintain, and despite having done the tutorial, I'm feeling kind of lost when I am wading through all this project code and have just a general l

Solution 1:

Django takes your html files and fills in the blanks.

you tell it where the blanks are using double brackets {{ somevar }} or one of the many built-in tags (such as 'for') which look like this {% sometag %}. It takes some time learning how to work with this, and learning about all the options you have (the docs are great), but it's very easy to understand the basics. Here are a bunch of examples:

views.py

defexample(request):
    template = 'index.html'
    context = {'myvar': 'hello world'}
    return render_to_response(template, context)

index.html

<body>
  <h1> {{ myvar }} </h1>    
</body>

result:

<body>
  <h1> hello world </h1>    
</body>

as you can see, render_to_response takes a template name and then fills in the blanks. It takes index.html, looks at the context dictionary, finds every appearence of {{ myvar }} and replaces that with hello world.

Say I have a model named MyModel which has a field MyData? To display it, simply send it with context:

def example(request):
    template = 'index.html'
    context = {'myvar': MyModel.objects.get(pk=1)}
    return render_to_response(template, context)

index.html

<body>
  <h1> {{ myvar.MyData }} </h1>    
</body>

Second example:

views.py

defexample_fortag(request):
    template = 'fortag.html'
    context = {'mylist': [1, 2, 3]}
    return render_to_response(template, context)

fortag.html

<body>
{% for item in mylist %}
    <h1>{{ item }}</h1>
{% endfor %}  
</body>

The result would look like this:

<body>
  <h1>1</h1>
  <h1>2</h1>
  <h1>3</h1> 
</body>

Get it? Here's an example using the if tag:

views.py:

defexample_iftag(request):
    template = 'iftag.html'
    context = {'mylist': range(1, 10)}
    return render_to_response(template, context)

iftag.html:

<body>
{% for item in mylist %}
    {% if item / 2 == 0 %} 
        <h1>{{ item }}</h1>
    {% endif %}
{% endfor %}  
</body>

The result would look like this:

<body>
  <h1>2</h1>
  <h1>4</h1>
  <h1>6</h1>
  <h1>8</h1>
</body>

You can also do complicated stuff. Say you send in a dictionary as a variable:

context = {'myvar': {'key': 'value'}}

you can do this:

{% forkey, value in myvar.items %}
 ... etc ...

See? I'm using the items function, which is a built-in of the dictionary object in python language (note I don't need to call the function with ()).

With all that in mind, I strongly suggest you go over the third part of the tutorial one more time and pick it up slowly. Django's template language is pretty intuitive and very powerful, hope this is helpful

Solution 2:

first you need to pass that variable to template in context. But if its already there then something like

{{ currentStudent.age }}

will work just fine

Post a Comment for "How To Display One Field From A Model In Django Template"