Tuesday, January 19, 2016

Re: regular expression confusion

On Tue, Jan 19, 2016 at 2:30 AM, Xristos Xristoou <saxri89@gmail.com> wrote:
hello,

i have a regular expression confusion error i cant work my app with together 2 and 3 line from my blog/urls if i work regardless that lines work fine
but i need together for my project.

my mysite/urls

url(r'^admin/', include(admin.site.urls)),
url(r'', include('blog.urls')),

The second line should probably be a r'^', rather than just an empty string.
 
my blog/urls

1  url(r'^$', views.index, name='index'),
2 url(r'^(?P<slug>[^\.]+)/$', views.view_post, name='view_post'),
3 url(r'^(?P<slug>[\w-]+)/$', views.view_category, name='view_category')

This is probably not a good URL strategy. Your view_category URL's overlap with your view_post URL's, which means that your view_category URL's will never work because [\w-]+ is contained within [^\.]+, which matches any character except for a \ and period, neither of which will ever be contained in a common slug format (used by view_category).

Wouldn't it be better and more obvious to namespace the URL's so that what you are matching is much more clear? Otherwise you'll have no way of telling a post apart from a category.
 
2  url(r'^post/(?P<slug>[^\.]+)/$', views.view_post, name='view_post'),
3 url(r'^category/(?P<slug>[\w-]+)/$', views.view_category, name='view_category')

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciXf%2BVWCDa%2BCER3ZrNRK%2Bwe60Cs34%3DdyLD8MSRXRd9r2hg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment