user3854113
user3854113

Reputation:

Django rendering field not working

My model:

class preguntas(models.Model):
pregunta = models.CharField(max_length=200)

My views:

opciones = preguntas.objects.all()
return render_to_response(template, {"pregunta": pregunta, "opciones": opciones})

Supposedly if in the template I write {{ opciones.pregunta }} it should return the field questions , but it doesnt , any idea ?

If you need more info tell me

Upvotes: 0

Views: 72

Answers (2)

walexnelson
walexnelson

Reputation: 424

It looks like you're passing in a list of preguntas as opciones. So you need to iterate over the list in order to read the corresponding pregunta.

{% for p in opciones %}
    {{ p.pregunta }} <!--do something with each pregunta-->
{% endfor %}

EDIT: souldeux beat me to it.

Upvotes: 1

souldeux
souldeux

Reputation: 3755

You load a queryset opciones but don't seem to load anything called pregunta before listing it in your context variables. pregunta is an attribute of an instance of an individual member of the opciones queryset.

Since opciones is a queryset, you would need to iterate through it to see each individual item in your template. Your template should look more like:

{% for o in opciones %}
{{ o.pregunta }}
{% endfor %}

Also, I believe when you use render_to_response you should also add context_instance=RequestContext(request) in order for things to work 100% smoothly.

Upvotes: 1

Related Questions