Opened 7 years ago

Last modified 23 hours ago

#29446 assigned New feature

Include template context in error page when relevant

Reported by: Shai Berger Owned by: Bhupesh panwar
Component: Error reporting Version: 2.0
Severity: Normal Keywords: template debug
Cc: Min ho Kim, Vibhu Agarwal Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: yes

Description

When an error is encountered while rendering a template (in Debug mode), Django helpfully displays the template source, followed by the usual stack-trace with local variable values. It could be useful to also include the template context at the point of the error, as these are often the relevant "local variables".

While the context is actually often available as a local variable in one of the function calls close to the bottom of the stack trace, it is not obvious, and requires a somewhat tedious search (there are usually a series of calls taking a context variable, most of them irrelevant).

According to the ticket's flags, the next step(s) to move this issue forward are:

  • To provide a patch by sending a pull request. Claim the ticket when you start working so that someone else doesn't duplicate effort. Before sending a pull request, review your work against the patch review checklist. Check the "Has patch" flag on the ticket after sending a pull request and include a link to the pull request in the ticket comment when making that update. The usual format is: [https://github.com/django/django/pull/#### PR].

Change History (14)

comment:1 by Tim Graham, 7 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Zach Bresser, 7 years ago

Shai,

Can you elaborate on what you mean? Are you suggesting we put the local context variables in a special section instead of with the rest of the local variables?

comment:3 by Min ho Kim, 6 years ago

Hi Shai Berger,
Could you give some more information about this feature request?
It would be nice to have some example of error page you would like to see.

If anyone else has any idea what the expected error page should look like, please comment.

comment:4 by Min ho Kim, 6 years ago

Cc: Min ho Kim added

comment:5 by Vibhu Agarwal, 5 years ago

Cc: Vibhu Agarwal added

comment:6 by Bhupesh panwar, 2 weeks ago

Owner: set to Bhupesh panwar
Status: newassigned

comment:7 by Bhupesh panwar, 2 weeks ago

Hi, I am bhupesh Panwar, i recently reproduce this issue.I have understand the issue very well. So i have a solution that can we create an middleware.py file that logs template context when error happens

in reply to:  7 ; comment:8 by Shai Berger, 12 days ago

First of all, I apologize for missing the calls for more details all these years ago.
Somehow, I only saw them now.

Second,

Replying to Bhupesh panwar:

So i have a solution that can we create an middleware.py file that logs template context when error happens

I think it would be more helpful to put the context in the error page with all the other relevant info, but if you can pick the right context and put it in the log, then IMO:

  • It's a good first step towards putting it on the page
  • It's already helpful in and of itself

comment:9 by Antoliny, 12 days ago

Has patch: set

in reply to:  8 comment:11 by Bhupesh panwar, 11 days ago

Replying to Shai Berger:
Thank you for reply, Shai, I have already raise an pr.if there might be some issue. i will keep you posted

Last edited 11 days ago by Bhupesh panwar (previous) (diff)

comment:12 by Mike Edmunds, 10 days ago

Patch needs improvement: set

comment:13 by Bhupesh panwar, 29 hours ago

I created a new pull request because my previous one had some issues. Here is the link: https://github.com/django/django/pull/19282 .

The issue is about retrieving the context (dictionary) from the user's views.py. However, I couldn't find a way to retrieve the context from views.py since the context variable name can vary depending on the user.

To illustrate this, I created a context.py file as an example. Could you suggest how we can dynamically retrieve the context variable from views.py?

comment:14 by Mike Edmunds, 23 hours ago

Has patch: unset
Patch needs improvement: unset
Note: See TracTickets for help on using tickets.
Back to Top