0

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 lack of understanding as soon as I hit templates.

I have a model called Student, defined like

class Student(models.model);
True
studentID = models.CharField(max_length = 6, primary_key = True)
age = models.IntegerField()

along with a few other fields omitted for brevity.

Now, I have a HTML file for the webpage on which I want to display the current student's score (current student is defined as the one currently accessing the page, identified by their student ID) in views.py, I get the current student by

currentStudent = Student.objects.get(studentID = request.session['studentID'])

My question: in my HTML file, how do I display the current student's age in part of a text block?

Michelle
  • 663
  • 1
  • 8
  • 17

2 Answers2

4

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

def example(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

def example_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:

def example_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:

{% for key, 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

yuvi
  • 18,155
  • 8
  • 56
  • 93
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

Odif Yltsaeb
  • 5,575
  • 12
  • 49
  • 80
  • Can you please give an example of how to "pass that variable to template in context"? – Michelle Dec 27 '13 at 20:30
  • I think you should work your way through the tutorial. Relevant part of tutorial is here : https://docs.djangoproject.com/en/dev/intro/tutorial03/#a-shortcut-render. – Odif Yltsaeb Dec 27 '13 at 20:53