Ask Your Question

How to show texts on views

asked 2013-06-10 09:41:24 -0500

Leonardo Afonso gravatar image

updated 2013-06-12 07:52:08 -0500


i'm trying put some texts and forms in a view on Openstack Horizon Dashboard, so , i was trying do it using the method rende_to_response from django.shortcuts on python code and the view don't show nothing .

my code is this.

from django import http
from django.utils.translation import ugettext_lazy as _
from django.views.generic import TemplateView
from django.shortcuts import render_to_response

class IndexView(TemplateView):
    template_name = 'visualizations/validar/index.html'

    def index(self):
        text = "it's a little text to do a test"
        return self.render_to_response('visualizations/validar/index.html', 'text':text})

and in my template like this

{% block main %}
 <h3> The text is : {{ text }}</h3>
{% endblock %}

It's a simple code, just to test this method.

and my file like this

from django.conf.urls.defaults import patterns, url
from .views import IndexView

urlpatterns = patterns('',
    url(r'^$', IndexView.as_view(), name='index')
edit retag flag offensive close merge delete


Can you be more specific as to what or how it doesn't work? What other steps did you follow (e.g. did you add your view to, etc?)

jpichon gravatar imagejpichon ( 2013-06-11 04:54:36 -0500 )edit

when i say that it didn't work, i want say that the view don't show nothing. I add the in the question.

Leonardo Afonso gravatar imageLeonardo Afonso ( 2013-06-12 07:53:48 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-06-13 05:20:49 -0500

jpichon gravatar image

It's a bit difficult to debug because the problem is still somewhat unclear. When you say, "the view don't show nothing", do you mean it doesn't load, or it shows an error 404, or it shows a blank page, or it shows your template with the "The text is" but not the variable content?

There are a few issues with your example that would help if it's the latter. You shouldn't need to use render_to_response within a TemplateView. If you want to show your text variable you would have to do something like this:

from django.views.generic import TemplateView

class IndexView(TemplateView):
    template_name = ''visualizations/validar/index.html''

    def get_context_data(self):
        text = "it's a little text to do a test"
        return {'text': text}

See for more information on the TemplateView class.

edit flag offensive delete link more


Yes jpichon, show the template, but not the variable content! I did what you said and works ... Thanks a lot!!

Leonardo Afonso gravatar imageLeonardo Afonso ( 2013-06-13 08:35:05 -0500 )edit

Glad this helped!

jpichon gravatar imagejpichon ( 2013-06-13 08:40:17 -0500 )edit

Get to know Ask OpenStack

Resources for moderators

Question Tools


Asked: 2013-06-10 09:41:24 -0500

Seen: 137 times

Last updated: Jun 13 '13