Legibilidade e reaproveitamento de código na "URLConf"
É comum nas configurações de URL (URLconf) [1] do Django, patterns como este abaixo:
urlpatterns = patterns(
'django.views.generic.date_based',
(r'^(?P<year>\d{4})/(?P<month>[0-9]{2})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/$',
'object_detail', dict(info_dict, slug_field='slug', month_format='%m')),
(r'^(?P<year>\d{4})/(?P<month>[0-9]{2})/(?P<day>\d{1,2})/$',
'archive_day', dict(info_dict, month_format='%m')),
(r'^(?P<year>\d{4})/(?P<month>[0-9]{2})/$',
'archive_month', dict(info_dict, month_format='%m')),
(r'^(?P<year>\d{4})/$', 'archive_year', info_dict),
(r'^/?$', 'archive_index', dict(info_dict, num_latest=5)),
)
Apesar da modificação nas URLs serem raras, a legibilidade da forma acima, queira ou não, prejudica numa eventual manutenção e a probabilidade de ocorrer um erro aumenta.
Uma solução que ao meu ver facilita muito a leitura seria utilizando a função url(), como no código abaixo, retirado do django-fleshin:
photo_detail = url(
regex = '^(?P<album>[-\w]+)/(?P<slug>[-\w]+)/$',
view = 'fleshin.views.photo_detail',
kwargs = dict(photo_info_dict, slug_field='slug'),
name = 'fleshin-photo'
)
photo_list = url( # all photos + album list
regex = '^$',
view = 'django.views.generic.list_detail.object_list',
kwargs = dict(photo_info_dict, paginate_by=FLESHIN_NUM_LATEST,
extra_context={'album_list': Album.objects.all}),
name = 'fleshin-photo-list'
)
photo_album_list = url( # only photos in ``album``
regex = '^(?P<album>[-\w]+)/$',
view = 'fleshin.views.photo_list',
kwargs = dict(photo_info_dict, paginate_by=FLESHIN_NUM_LATEST),
name = 'fleshin-photo-album-list'
)
urlpatterns = patterns('', photo_detail, photo_list, photo_album_list)
Perceba também que a forma acima facilita a reutilização dos patterns pelo seu projeto, não ficando preso ao que foi definido no django-fleshin, reaproveitando, por exemplo, o mesmo padrão de URL para o detalhamento de uma Photo (photo_detail) e alterando o padrão para a listagem das mesmas (photo_list e photo_album_list).
| [1] | Geralmente encontrado em um arquivo de nome urls.py. |
Comentários
<a href=http://drugstorerx.medsjoy.biz>Buy Viagra</a>
Get emergency medical help if you have any of these signs of an allergic reaction: hives; difficulty breathing; swelling of your face, lips, tongue, or throat. During sexual activity, if you become dizzy or nauseated, or have pain, numbness, or tingling in your chest, arms, neck, or jaw, stop and call your doctor right away. You could be having a serious side effect of Viagra. <a href=http://discountsrxpills.com>Viagra</a>
<a href=http://hqrxmed.com>Online Viagra</a>
<a href=http://toprxed.wetpaint.com/page/Top+Online+Pharmacy>Online Pharmacy Viagra</a>
<a href=http://hqrxmed.com/propecia.htm>Online Propecia</a>
<a href=http://hqrxmed.com/cialis.htm>Buy Cialis</a>
<a href=http://buyviagrarx.mediaplace.biz>Buy Viagra On-Line</a>
<a href=http://viagrarxpills.mediaplace.biz>Online Viagra</a>
In another study in healthy male volunteers, coadministration with the HIV protease inhibitor ritonavir, which is a highly potent P450 inhibitor, at steady state (500 mg bid) with VIAGRA (100 mg single dose) resulted in a 300% (4-fold) increase in sildenafil Cmax and a 1000% (11-fold) increase in sildenafil plasma AUC. At 24 hours the plasma levels of sildenafil were still approximately 200 ng/mL, compared to approximately 5 ng/mL when sildenafil was dosed alone. This is consistent with ritonavir's marked effects on a broad range of P450 substrates. VIAGRA had no effect on ritonavir pharmacokinetics (see DOSAGE AND ADMINISTRATION). <a href=http://viagraacheter.mediaplace.biz>Acheter Viagra</a>
<a href=http://buyviagraline.mediaplace.biz>Buy Generic Viagra</a>
<a href=http://discountsrxpills.com>Buy Generic Viagra</a>
<a href=http://popularpills.mediaplace.biz>Buy Generic Viagra</a>
<a href=http://buy-cialis-rx.com>Buy Cialis On Line</a>
<a href=http://buy-propecia-rx.com>Buy Propecia Online</a>
<a href=http://buy-levitra-rx.net>Buy Generic Levitra</a>
<a href=http://buy-viagra-rx.net>Online Viagra</a>
<a href=http://buy-acomplia-rx.com>Buy Acomplia Online</a>
<a href=http://buy-cialis-rx.com>Lasix Online</a>
Viagra may also be used for other purposes not listed in this medication guide.
Deixar um comentário