sidewinder
sidewinder

Reputation: 3163

Pyramid Framework - Help with creating a view function

I have the following code for the view:

def pages_view(request):
    path = request.path.split('/')[1]
    dbsession = DBSession()
    page = dbsession.query(PagesTable).from_statement(
    'SELECT * FROM pages WHERE path=:page_path').params(page_path=path).first()
    pages_dir = os.getcwd() + '/myblog/templates/pages/'
    if page:
        if path == '':
            return render_to_response('myblog:templates/pages/home.mak',
            {'page':page}, request=request)
        elif os.path.isfile(pages_dir + path + '.mak'):
            return render_to_response('myblog:templates/pages/%s.mak'%path,
            {'page':page}, request=request)
        else:
            return render_to_response('myblog:templates/pages/index.mak',
            {'page':page}, request=request)
    raise NotFound()

Basically, it checks if a page exists in the table. If it does it renders a template according to the path name, or if there is no such template it just renders a default template.

The next part I want to do is create a seperate view function for my 'blog' page, with some logic for this page.

I've tried the following example, but it throws an undefined error when when I load the page:

@view_config(renderer='myblog:templates/pages/my-blog.mak')
def blog_view(request):
    one = 'Hello World'
    return {'one':one}

I do apologise for such basic questions. Any insight given will be greatly appreciated.

Here is the traceback:

URL: http://127.0.0.1:6543/my-blog/
    File '/Users/Awais/virtualenv/lib/python2.6/site-packages/WebError-0.10.3-py2.6.egg/weberror/evalexception.py', line 431 in respond
  app_iter = self.application(environ, detect_start_response)
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/repoze.tm2-1.0b1-py2.6.egg/repoze/tm/__init__.py', line 23 in __call__
  result = self.application(environ, save_status_and_headers)
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/pyramid-1.0-py2.6.egg/pyramid/router.py', line 158 in __call__
  response = view_callable(context, request)
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/pyramid-1.0-py2.6.egg/pyramid/config.py', line 2916 in _requestonly_view
  response = view(request)
File '/Users/Awais/virtualenv/MyBlog/myblog/views.py', line 25 in pages_view
  {'page':page}, request=request)
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/pyramid-1.0-py2.6.egg/pyramid/renderers.py', line 111 in render_to_response
  return helper.render_to_response(value, None, request=request)
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/pyramid-1.0-py2.6.egg/pyramid/renderers.py', line 322 in render_to_response
  result = self.render(value, system_values, request=request)
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/pyramid-1.0-py2.6.egg/pyramid/renderers.py', line 318 in render
  result = renderer(value, system_values)
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/pyramid-1.0-py2.6.egg/pyramid/mako_templating.py', line 131 in __call__
  result = template.render_unicode(**system)
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/Mako-0.3.6-py2.6.egg/mako/template.py', line 292 in render_unicode
  as_unicode=True)
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/Mako-0.3.6-py2.6.egg/mako/runtime.py', line 575 in _render
  **_kwargs_for_callable(callable_, data))
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/Mako-0.3.6-py2.6.egg/mako/runtime.py', line 607 in _render_context
  _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/Mako-0.3.6-py2.6.egg/mako/runtime.py', line 633 in _exec_template
  callable_(context, *args, **kwargs)
File 'myblog_templates_pages____base_mak', line 27 in render_body
File 'myblog_templates_pages_my_blog_mak', line 34 in render_body
File '/Users/Awais/virtualenv/lib/python2.6/site-packages/Mako-0.3.6-py2.6.egg/mako/runtime.py', line 181 in __str__
  raise NameError("Undefined")
NameError: Undefined

Upvotes: 2

Views: 1241

Answers (4)

Mark Hildreth
Mark Hildreth

Reputation: 43071

You would have been better off following through on this question rather than starting the other one. Based on information here and in the comment you provided in the other question, the following is taking place:

View:


def view1(request)
    args1 = 'a string'
    return render_to_response('page.mak',{'args1':args1}, request=request)

def view2(request)
    args2 = 'a string 2'
    return render_to_response('page.mak',{'args2':args2}, request=request)

You have the following in your 'page.mak' template file...

${args2}

When view1 is called, render_to_response tries to render the page.mak template. The dictionary {'args1'=args1} is used as context. When mako sees ${args2}, it looks up args2 in the context dictionary. The context dictionary constructed in view1 does not have args2 in the context dictionary, so it will cause an error. Rendering this same page in view2 works fine, because view2 is correctly passing the expected value with the key args2.

So, the error you're getting is because of trying to render a variable in your template that you never placed into the template context dictionary.

Upvotes: 1

Antoine Leclair
Antoine Leclair

Reputation: 18050

Like Mark Hildreth said in the comments of your original question, the error seems to be happening during the rendering of your template. You probably try to do something with a variable that does not exist/is not set in your view function.

In your first example, you always set a page variable, maybe that's the one missing.

Upvotes: 0

sidewinder
sidewinder

Reputation: 3163

Taking out the first part of the code, resolves the error.

Perhaps because render_to_response for my-blog.mak is occuring twice?

In the first part of the code you have

return render_to_response('myblog:templates/pages/%s.mak'%path, 

Which renders my-blog.mak if it exists,

The second time I do:

def blog_view(request):
variable = 'hello world'
return render_to_respomse('myblog:templates/pages/my-blog.mak', {'variable':variable}, request=request) 

The thing is I need something first renders the templates if they exist or not and are in the database table.

The second part is creating some seperate logic for that particular template.

Upvotes: 0

Rob Alarcon
Rob Alarcon

Reputation: 1450

It look's like you have configuration issues in your routing system, try to change the action to this, if it works maybe its a permission issue, could post the stack trace that the error raise?

def blog_view(request):
    one = 'Hello World'

    return render_to_response('myblog:templates/pages/my-blog.mak',
        {'one':one}, request=request)

Upvotes: 0

Related Questions