Re: Correct way to use a custom 404 view

2011-09-14 Thread Benjamin Sims
Thanks Michael, adding a view for the Exception context worked perfectly.
So, I did this in __init__.py:

from myapp.system import error_view
config.add_view(error_view, renderer = 'templates/error_template.pt',
context=Exception)

Ben

On 13 September 2011 23:41, Michael Merickel mmeri...@gmail.com wrote:

 The way to think about this is that Pyramid is at the end of a WSGI
 pipeline. It supports a way for catching and handling any exceptions that
 occur within Pyramid itself via exception views. You have seen one exception
 view already via the HTTPNotFound exception. You may add an exception view
 for anything raised within Pyramid, including HTTPForbidden,
 HTTPInternalServerError or even *Exception*. An exception view is invoked if
 an exception of that type is *raised*. Thus for your HTTPInternalServerError
 exception view to be invoked you'd need to raise HTTPInternalServerError
 somewhere in your code. Obviously the general way to add a view for all
 exceptions is to add a view for Exception, which uses your internal_error
 template and sets the response's status to 500. You can then further add
 more specific instances of Exception, like HTTPNotFound if you wanted a
 different error template (or status code) for that exception.

 Note that exception views only work for exceptions, if you *return*
 HTTPNotFound, the exception view will not be invoked and the returned value
 will be expected to be a conforming Response object (which it happens to
 be).

 Now, if an exception is not handled by Pyramid because you didn't add a
 view for it, then it will simply propagate up the WSGI stack until something
 else handled it. WebError is one such option. If left unhandled, the WSGI
 server will catch it and return a default 500 page (which is what you're
 seeing).

 Your INI setup is actually incorrect, because you didn't actually add the
 filter to the pipeline. Configuring a filter is only one step, you also must
 add it:

 [pipeline:main]
 pipeline =
   weberror
   tm
   myapp

 --

 Michael

 --
 You received this message because you are subscribed to the Google Groups
 pylons-discuss group.
 To post to this group, send email to pylons-discuss@googlegroups.com.
 To unsubscribe from this group, send email to
 pylons-discuss+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/pylons-discuss?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
pylons-discuss group.
To post to this group, send email to pylons-discuss@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-discuss+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.



Correct way to use a custom 404 view

2011-09-13 Thread Benjamin Sims
Hi,

I'm trying to generate a custom 404 view for my application. I've read:

https://pylonsproject.org/projects/pyramid/dev/narr/hooks.html#changing-the-notfound-view

However, I want to be able to have the 404 use a Chameleon template with
macros and so forth in order to fit in with the overall look and feel.

I have:

config.add_view(notfound_view, renderer = 'templates/view_not_found.pt',
context=HTTPNotFound)

in the __init__, and:

def notfound_view(request):
master = get_renderer('templates/macros_template.pt').implementation()
return dict(master = master)

as the relevant view. However, while it will correctly give me the 404 page
I want to see, the HTTP header will be a 200 OK.

I'm aware that I can do:

def notfound_view(request):
return HTTPNotFound()

however I then of course do not see my templated view.

Could somebody please expand on the documentation example and tell me what
calls I need to achieve this?

Thanks,
Ben

-- 
You received this message because you are subscribed to the Google Groups 
pylons-discuss group.
To post to this group, send email to pylons-discuss@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-discuss+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.



Re: Correct way to use a custom 404 view

2011-09-13 Thread Chris McDonough
On Tue, 2011-09-13 at 15:05 +0100, Benjamin Sims wrote:
 Hi,
 
 I'm trying to generate a custom 404 view for my application. I've
 read:
 
 https://pylonsproject.org/projects/pyramid/dev/narr/hooks.html#changing-the-notfound-view
 
 However, I want to be able to have the 404 use a Chameleon template
 with macros and so forth in order to fit in with the overall look and
 feel.
 
 I have:
 
 config.add_view(notfound_view, renderer =
 'templates/view_not_found.pt', context=HTTPNotFound)
 
 in the __init__, and:
 
 def notfound_view(request):
 master =
 get_renderer('templates/macros_template.pt').implementation()
 return dict(master = master)


def notfound_view(request):
request.response.status_int = 404
master get_renderer('templates/macros_template.pt').implementation()
return dict(master = master)

It's slightly different in 1.0, but the above works in 1.1 and 1.2.

- C


-- 
You received this message because you are subscribed to the Google Groups 
pylons-discuss group.
To post to this group, send email to pylons-discuss@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-discuss+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.



Re: Correct way to use a custom 404 view

2011-09-13 Thread Benjamin Sims
Thanks Chris, that works great.

Sorry for the follow on question, but is there a similar way to handle
Python errors/500?

I have set up pyramid_exclog to send any errors to me by email. I also have
the following lines in my ini:


[pipeline:main]
pipeline =
tm
myapp

[filter:weberror]
use = egg:WebError#error_catcher
debug = false

I now get a correct 500 error and a message saying 'Internal Server Error'.
However, I would like to customise that error so that it works in a similar
way to the 404.

I tried adding this to the __init__:

from pyramid.httpexceptions import HTTPInternalServerError
from myapp.system import internal_error_view
config.add_view(internal_error_view, renderer = 'templates/
internal_error.pt',
context=HTTPInternalServerError)

However, this does not work (which as I understand it makes sense, since the
above indicates that the error has already gone out to paster).

Is there a catch-all way to catch the errors in Pyramid and use a fancy
error page?

Thanks,
Ben

On 13 September 2011 15:21, Chris McDonough chr...@plope.com wrote:

 On Tue, 2011-09-13 at 15:05 +0100, Benjamin Sims wrote:
  Hi,
 
  I'm trying to generate a custom 404 view for my application. I've
  read:
 
 
 https://pylonsproject.org/projects/pyramid/dev/narr/hooks.html#changing-the-notfound-view
 
  However, I want to be able to have the 404 use a Chameleon template
  with macros and so forth in order to fit in with the overall look and
  feel.
 
  I have:
 
  config.add_view(notfound_view, renderer =
  'templates/view_not_found.pt', context=HTTPNotFound)
 
  in the __init__, and:
 
  def notfound_view(request):
  master =
  get_renderer('templates/macros_template.pt').implementation()
  return dict(master = master)


 def notfound_view(request):
request.response.status_int = 404
 master get_renderer('templates/macros_template.pt').implementation()
return dict(master = master)

 It's slightly different in 1.0, but the above works in 1.1 and 1.2.

 - C


 --
 You received this message because you are subscribed to the Google Groups
 pylons-discuss group.
 To post to this group, send email to pylons-discuss@googlegroups.com.
 To unsubscribe from this group, send email to
 pylons-discuss+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/pylons-discuss?hl=en.



-- 
You received this message because you are subscribed to the Google Groups 
pylons-discuss group.
To post to this group, send email to pylons-discuss@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-discuss+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.



Re: Correct way to use a custom 404 view

2011-09-13 Thread Michael Merickel
The way to think about this is that Pyramid is at the end of a WSGI
pipeline. It supports a way for catching and handling any exceptions that
occur within Pyramid itself via exception views. You have seen one exception
view already via the HTTPNotFound exception. You may add an exception view
for anything raised within Pyramid, including HTTPForbidden,
HTTPInternalServerError or even *Exception*. An exception view is invoked if
an exception of that type is *raised*. Thus for your HTTPInternalServerError
exception view to be invoked you'd need to raise HTTPInternalServerError
somewhere in your code. Obviously the general way to add a view for all
exceptions is to add a view for Exception, which uses your internal_error
template and sets the response's status to 500. You can then further add
more specific instances of Exception, like HTTPNotFound if you wanted a
different error template (or status code) for that exception.

Note that exception views only work for exceptions, if you *return*
HTTPNotFound, the exception view will not be invoked and the returned value
will be expected to be a conforming Response object (which it happens to
be).

Now, if an exception is not handled by Pyramid because you didn't add a view
for it, then it will simply propagate up the WSGI stack until something else
handled it. WebError is one such option. If left unhandled, the WSGI server
will catch it and return a default 500 page (which is what you're seeing).

Your INI setup is actually incorrect, because you didn't actually add the
filter to the pipeline. Configuring a filter is only one step, you also must
add it:

[pipeline:main]
pipeline =
  weberror
  tm
  myapp

-- 

Michael

-- 
You received this message because you are subscribed to the Google Groups 
pylons-discuss group.
To post to this group, send email to pylons-discuss@googlegroups.com.
To unsubscribe from this group, send email to 
pylons-discuss+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.