(Context: I’m still very new to both Django and web development.)
1) Could someone explain the reasoning for the following statement? This Q/A answered the question of how to deal with it, but not why it might be a good/bad idea.
The docs state, “The page_not_found view should
suffice for 99% of Web applications, but if you want to override it,
you can specify handler404 in your URLconf”.
That is, the page_not_found view only passes on the requested URL and ignores
any message you provide when raising an exception. It seems to me that
having the option to provide helpful hints to the 404.html template
by default would be good for everyone.
2) I’m currently making a custom view so that I can pass on helpful messages for the following situation. Is there some reason I shouldn’t?
I’m using matrix URLs so the base resource is
a normal hierarchical URL followed by matrix options in the basic
format of:
;filter_type1=item:value,item:value;filter_type2=item:value…
So it is quite easy to provide helpful messages based on how far along
the parsing gets before having an error. It seems helpful to me to pass
on a message such as the following:
- “Allowed filter_types are: type1, type2, type3.” or
- “Allowed items for filter_type a are: item1, item2, item3.”
Apologies if I missed this explanation elsewhere. I’ve looked and I asked on google django-users but got no replies.
I’m not really sure what the question is. The page_not_found view is basic by default because there aren’t that many situations where you need to explain why the page wasn’t found except that it wasn’t.
I presume by ‘matrix options’ you mean URL/GET parameters like:
Instead of throwing a 404 if they enter an incorrect combination of parameter, why not default to the base url/template (http://domain.com/page/) with an error message saying “Option1 can only be x,y,z”. This way they are presented with a familiar page and can retry their selection without having to navigate back from a 404 page (and it isn’t so confusing as to why it didn’t work).
You can use django’s messaging framework to easily accomplish this (I’ve done so in the past). Pass an error message to the template when you are checking the parameters in the view.