Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#24707 closed Cleanup/optimization (fixed)

urls: error messages for broken views are better for deprecated-old-style than they are for new-style

Reported by: Harry Percival Owned by: Harry Percival
Component: Core (URLs) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I found this out while updating my book to 1.8. It's a tiny thing really, but if you've accidentally made an uncallable view function, eg:

# views.py:
my_view = None

Contrast the behaviour of urls.py if you import it old-style and new-style:

# urls.py:
from myapp import views
urlpatterns = [
    url(r'/new-style-url', views.my_view)
    url(r'/old-style-url', 'my_app.views.my_view')
]

Attempting to use the dot-notation gives a helpful error message:

>>> resolve('/old-style-url')
[...]
ViewDoesNotExst: Could not import 'my_app.views.my_view'. View is not callable."

But the new-style one is rather cryptic:

Attempting to

>>> resolve('/whatever')
[...]
AttributeError: 'NoneType' object has no attribute 'rindex'

It's a shame the deprecated, old-style version has better UX than the new-style! I think it's a simple fix tho, I am working on a PR.

Change History (13)

comment:1 by Harry Percival, 9 years ago

OK have submitted a PR: https://github.com/django/django/pull/4563

I wasn't sure whether to do the PR against the 1.8.x branch or against master. I did the former but it'll apply against both...

comment:2 by Harry Percival, 9 years ago

Has patch: set
Owner: changed from nobody to Harry Percival
Status: newassigned

comment:3 by Harry Percival, 9 years ago

Here's the PR against master: https://github.com/django/django/pull/4564

comment:4 by Marten Kenbeek, 9 years ago

Component: UncategorizedCore (URLs)
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

Your PR should be against master. If it will be applied to 1.8, the patch will be backported.

It's not exactly a bug (the new-style syntax never had a nice error message I believe), but a better error message sure would be nice.

comment:5 by Harry Percival, 9 years ago

Thanks @knbk, PR against master submitted, https://github.com/django/django/pull/4564.

How might I argue for / help this PR to get backported into 1.8.x?

comment:6 by Markus Holtermann, 9 years ago

Needs documentation: set
Patch needs improvement: set

comment:7 by Markus Holtermann, 9 years ago

Needs documentation: unset
Version: 1.8master

If you can convince a core developer that the error is at least one of

  • a security fix
  • data loss bug
  • crashing bug
  • major functionality bugs in a newly-introduce feature
  • or a regression from older version of Django

I don't see any of them to be the case, so, it's going to be 1.9+

comment:8 by Harry Percival, 9 years ago

The only one I think might qualify is that it's a sort-of regression. If we're saying that the old-stye url patterns with dot-notation are deprecated in favour of direct imports, then in that sense, there's a regression in the UX. In the old-style, I get good, helpful error messages. In the new-style, I get unhelpful error messages when I make a mistake...

comment:9 by Harry Percival, 9 years ago

From a "selfish" point of view, the reason I've come to this is -- in my book, my intro to django urls and views comes via a test (it's a TDD book). Using old-style urls, I show how django's error messsages guide us gently towards the right answer. Moral of the story is, yay django, yay tests. I'm trying to update it for 1.8, and use the recommended direct imports, but because the error messages aren't as good, the story really doesn't work so well.

comment:10 by Markus Holtermann, 9 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

Well, that's still not a regression as the recommendation to use callable objects instead of string was first made in 1.7: https://docs.djangoproject.com/en/1.7/topics/http/urls/#passing-strings-instead-of-callable-objects

comment:11 by Harry Percival, 9 years ago

It's definitely a grey area, I agree. Still, if you're prepared to entertain the argument that it's a maybe-sortof-regression-in-the-ux, then the fact that the deprecation happened in 1.7 doesn't matter, it's still a regression... I guess, in theory, you could apply to it to 1.7.x as well as 1.8.x!? Anyways, irrespective of what the specific guidelines are, I'd still like to asking you guys to consider it. It really would make my life lots easier for the move to 1.8, and it'll help me tell a better story to my readers, lots of which are meeting django for the first time. So if there's some leeway to interpret the rules and make someone happy, well...

comment:12 by Markus Holtermann <info@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In 40768ec2:

Fixed #24707 -- Improved error reporting for explicitly imported uncallable views

comment:13 by Harry Percival, 9 years ago

re: the possibility of a backport, if it's any help, here's another case where there was a grey area:

https://code.djangoproject.com/ticket/24635

strictly speaking, that wasn't a security fix or a regression or a major bug, just a tweak to the UX to accomodate the fact that the old url syntax is deprecated. and it got backported...

Note: See TracTickets for help on using tickets.
Back to Top