So, time for a newbie question but so utterly important since the documentation seems to have missed this very very basic example.
All I’m trying to do is to return a value from a model to a view in Django. Here is some code.
The model
class Page(models.Model):
def index(self):
ex = 'foo string'
return ex
The view
def index(request):
start = Page.objects.all().index
#start = Page.index
return render_to_response('index.html', {'page_content': start})
And, nothing gets rendered. I get this error message with <unbound method Page.index>
…
The funny thing is that if I skip the functions and so on and creates the model like this
class Page(models.Model):
ex = 'goo string'
And call it with
start = Page.ex
Everything renders out fine.
Any pointer or working example would be much appreciated!
Thanks
This sort of thing is rightly left out of the Django tutorial, since it has nothing to do with Django. If you don’t know basic Python, then you need to follow a Python tutorial.
The main thing wrong with the code you gave is that you haven’t actually called the index method, you’ve just referred to it. In Python, as in many other languages, to call a method you need to use
method(). So you need: