Ask Your Question
0

How to show texts on views

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

Leonardo Afonso gravatar image

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

Hi,

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 urls.py 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

Comments

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 urls.py, etc?)

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

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

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

1 answer

Sort by ยป oldest newest most voted
0

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

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 https://docs.djangoproject.com/en/1.4/ref/class-based-views/#django.views.generic.base.TemplateView for more information on the TemplateView class.

edit flag offensive delete link more

Comments

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 -0600 )edit

Glad this helped!

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Get to know Ask OpenStack

Resources for moderators

Question Tools

Stats

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

Seen: 115 times

Last updated: Jun 13 '13