Code

Opened 6 years ago

Closed 6 years ago

#6253 closed (wontfix)

reverse() in tutorial part 4 should use the function itself rather than the string representation

Reported by: alberto@… Owned by: nobody
Component: Documentation Version: master
Severity: Keywords:
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

In the vote() function example, at the tutorial part 4 (http://www.djangoproject.com/documentation/tutorial04/), it's better to use

def vote(request, poll_id):
  ...
  return HttpResponseRedirect(reverse(results, args=(p.id,)))

rather then

def vote(request, poll_id):
  ...
  return HttpResponseRedirect(reverse('mysite.polls.views.results', args=(p.id,)))

to decouple the polls app from the mysite project.

Attachments (0)

Change History (3)

comment:1 Changed 6 years ago by SmileyChris

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from Decouple app from project in vote() function example - tutorial part 4 to reverse() in tutorial part 4 should use the function itself rather than the string representation
  • Triage Stage changed from Unreviewed to Design decision needed

There are more places than this needed to fix to decouple the app.

That said, this specific change isn't a bad idea, is it?

comment:2 Changed 6 years ago by ubernostrum

Probably the most reliable thing is to use a named pattern; that would get people into the habit of naming their patterns which, in turn, reduces the potential for problems from multiple URLs using the same view function.

comment:3 Changed 6 years ago by mtredinnick

  • Resolution set to wontfix
  • Status changed from new to closed

This change would be a bad idea. Using reverse() against function objects is very fragile. The problem is that they are automatically imported under different paths and it is officially Very Hard(tm) in Python to tell when they are the same object (short of md5-summing the code objects, which seems like overkill). And, really, I don't care very much that that's case, since using named URL patterns is safer and saner on a number of levels.

Eventually, there's a bit of a sanity pass needed over the code samples in the tutorial to bring them all into alignment and using URL patterns (after introducing them earlier) is probably the best idea. However, the change suggested here, although well intentioned actually leads to subtle trouble.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.