[Django] #24143: Documentation uses uninstantiated Http404s

17 views
Skip to first unread message

Django

unread,
Jan 13, 2015, 3:18:31 AM1/13/15
to django-...@googlegroups.com
#24143: Documentation uses uninstantiated Http404s
--------------------------------------+--------------------
Reporter: kezabelle | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+--------------------
I've always used instantiated Http404 exceptions in my stuff, but it
doesn't seem to be a well-defined practice, and isn't the demonstrated way
in the documentation.

In `DEBUG`, when an `Http404` is encountered it is re-routed to the 404
handler, which renders using `TECHNICAL_404_TEMPLATE`, and an opportunity
arises to provide hints about what code was causing the error, much like I
asked for in #22756 (if there's a theme to my tickets, it's that I value
debugging simplicity), which puts the view causing the 404 into the
result. By suggesting instantiating 404s with messages, we can help users
help themselves further.

Given this view:
{{{
def myview(request, obj_id):
obj = get_object_or_404(MyModel, pk=obj_id)
if not mymodel.published:
Http404("Unpublished")
if not mymodel.is_accessible_via(request.method):
Http404("Not via this method")
if not mymodel.allows_user(request.user):
Http404("Not found for this user")
# ...
}}}
It is easy to see which 404 was thrown in the technical debug view because
`force_bytes` is called on the exception (which inherits `__str__` from
`Exception`) and the `reason` is rendered as the error message if no
`urlpatterns` were tried.

Without instantiating (`raise Http404` vs `raise Http404('message')`) it
becomes a debugging guessing game as to which condition threw the
exception (ignoring the merit of the above conditions, which are total
garbage)

I'm suggesting that the places in the documentation where bare, un-
instantiated Http404s are raised is updated to reflect this idea, which is
already used in places like `get_object_or_404` anyway.

--
Ticket URL: <https://code.djangoproject.com/ticket/24143>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 13, 2015, 3:22:58 AM1/13/15
to django-...@googlegroups.com
#24143: Documentation uses uninstantiated Http404s
-------------------------------------+-------------------------------------
Reporter: kezabelle | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by kezabelle):

* needs_better_patch: => 0
* has_patch: 0 => 1
* needs_tests: => 0
* needs_docs: => 0


Comment:

Pull request is https://github.com/django/django/pull/3907

--
Ticket URL: <https://code.djangoproject.com/ticket/24143#comment:1>

Django

unread,
Jan 15, 2015, 11:03:36 AM1/15/15
to django-...@googlegroups.com
#24143: Documentation uses uninstantiated Http404s
--------------------------------------+------------------------------------

Reporter: kezabelle | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timgraham):

* needs_better_patch: 0 => 1
* stage: Unreviewed => Accepted


Comment:

As I mentioned on the PR, I think this ticket's rationale could be added
to the docs as well.

--
Ticket URL: <https://code.djangoproject.com/ticket/24143#comment:2>

Django

unread,
Jan 16, 2015, 3:54:27 AM1/16/15
to django-...@googlegroups.com
#24143: Documentation uses uninstantiated Http404s
-------------------------------------+-------------------------------------
Reporter: kezabelle | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by berkerpeksag):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin


Comment:

[https://github.com/django/django/pull/3907 PR #3907] LGTM.

--
Ticket URL: <https://code.djangoproject.com/ticket/24143#comment:3>

Django

unread,
Jan 16, 2015, 9:42:05 AM1/16/15
to django-...@googlegroups.com
#24143: Documentation uses uninstantiated Http404s
-------------------------------------+-------------------------------------
Reporter: kezabelle | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Documentation | Version: master
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"726a9550db5129badc1c44809b0bed728fa1ad90"]:
{{{
#!CommitTicketReference repository=""
revision="726a9550db5129badc1c44809b0bed728fa1ad90"
Fixed #24143 -- Encouraged use of Http404 messages for debugging.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24143#comment:4>

Django

unread,
Jan 16, 2015, 9:42:19 AM1/16/15
to django-...@googlegroups.com
#24143: Documentation uses uninstantiated Http404s
-------------------------------------+-------------------------------------
Reporter: kezabelle | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Documentation | Version: master

Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"bd08cfca6ff04e7cec940f5b59e97cdcceddcc69"]:
{{{
#!CommitTicketReference repository=""
revision="bd08cfca6ff04e7cec940f5b59e97cdcceddcc69"
[1.7.x] Fixed #24143 -- Encouraged use of Http404 messages for debugging.

Backport of 726a9550db5129badc1c44809b0bed728fa1ad90 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24143#comment:5>

Reply all
Reply to author
Forward
0 new messages