I have a Django app which when presented with some user error (i.e URL does not exist or no permissions), it will do messages.add_message. The message contains a link to an explanation of the error at /error/<id>. If I want to re-use the error id and message, how do I do it? I was thinking something like this:
errors = {1 : "Error message for error id 1", 2 : "Error message for error id 2"}
Where could I store such a dictionary so that I can access it in all of my views?
You should create a view that maps to a
urllike/error/<id>. Then inside the view have the dictionaryerrors = {1 : "Error message for error id 1", 2 : "Error message for error id 2"}or alternatively inside a file callederror_codes.pyand import it into yourviews.py. Then simply parse the<id>passed in theurland return atemplatewith the correcterror code.To ensure that this dictionary of error codes is available across all your requests, use write custom Django Middleware. Implement
process_template_response(self, request, response)and alter theresponse.context_databy adding yourerror_code_dictionaryto it. Will ensure that every response has the error dictionary available across your http responses rendered in templates and elsewhere.