Opened 13 years ago

Closed 13 years ago

#4087 closed (duplicate)

Decoupling the URLconfs is not complete

Reported by: anonymous Owned by: Jacob
Component: Documentation Version: master
Severity: Keywords: decouple urlconf view
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


The example of Decoupling the URLconfs in Tutorial 03 is not a very good one.

urlpatterns = patterns('mysite.polls.views',
    (r'^$', 'index'),
    (r'^(?P<poll_id>\d+)/$', 'detail'),
    (r'^(?P<poll_id>\d+)/results/$', 'results'),
    (r'^(?P<poll_id>\d+)/vote/$', 'vote'),

While the 'mysite.polls...' stuff remains, the URLconfs is not fully decoupled from the views. How about using the function names and relative import?

from .views import index, detail, results, vote

urlpatterns = patterns('',
    (r'^$', index),
    (r'^(?P<poll_id>\d+)/$', detail),
    (r'^(?P<poll_id>\d+)/results/$', results),
    (r'^(?P<poll_id>\d+)/vote/$', vote),

Better decoupling, isn't it?

Change History (3)

comment:1 Changed 13 years ago by James Bennett

Resolution: duplicate
Status: newclosed

"Decoupled" in this case does not mean "has no knowledge of anything else", it means "does not hard-code the full URL path". After this step of the tutorial, the polls app's URLs can be relocated to anywhere within the site's URL structure -- this is very different from "the code can be relocated to anywhere on the server".

Also, recommending relative imports seems like a bad idea; the syntax you're using only works in Python 2.5 (and then only with from __future__ import absolute_import), while Django needs to stay compatible back to Python 2.3 (where relative imports are dangerous; in Python 2.5 and later they're just bad practice...).

Finally, this is a duplicate of #3941, which has been closed for much the same reason; in the future, please search the ticket tracker to avoid filing duplicate tickets.;

comment:2 Changed 13 years ago by anonymous

Resolution: duplicate
Status: closedreopened

Thanks for the reply. I still doubt about that. When something like 'mysite.polls.views' is hard-coded in URLconf, you just can't drop-in and use the app in another project. If there's a lot of apps like that, you'll have to change the prefix of views in URLconf one by one, annoyingly and fallibly.

I'm sorry for my infamiliarity with prior version of Python. In my opion, explicit relative import is a good thing. For compatibility, that snippet of code may be marked as "used in python 2.5 and later only", while being given as an example of better decoupling.

ps: As far as I know, in Python 2.5, the syntax form .xxx import yyy can be use without from __future__ import absolute_import. The latter statement only asserts that "each module without a leading dot is absolute".

comment:3 Changed 13 years ago by Chris Beaven

Resolution: duplicate
Status: reopenedclosed

Hi anonymous. Please bring this up on the developers group rather than reopening and debating it here.

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