I have urls where spaces are replaced with the "-" character.
So I made a url regex like this:
url(r'^(?P<item_url>(\w+-?)*)/$', 'detail'),
my view:
def detail(request, item_url):
i = get_object_or_404(Page, url=item_url,published=True)
return render_to_response('item/detail.html', {'item':i},
context_instance=RequestContext(request))
Unfortunately this keeps django extremely busy on urls with more than 20 characters. The process hangs for 20sec - 1 minute and then returns the correct result. Is this based on a wrong regex I'm using?
Try the following url pattern:
url(r'^(?P<item_url>[\w-]+)/$', 'detail'),
[\w-]+ will match one or more alphanumeric characters or hyphens.
Related
I have created the following urlpatters
urlpatterns=[
url(r'(?P<user_name>[a-zA-Z]+)/$', views.profile_view,
kwargs=None, name='userprofile'),
url(r'(?P<user_name>[a-zA-Z]+)/interests/$',views.interest,name='interests')
]
But when I enter the url localhost:8000/mainuser/interest/ it treat it as first url and opens profile_view. It is clearly matching '/'. Help me with this.
You should begin your URL patterns with the start of line character ^. Because you are not including this character the regex for the first pattern matches any URL that ends with 1 or more characters followed by a forward-slash.
urlpatterns=[
url(r'^(?P<user_name>[a-zA-Z]+)/$', views.profile_view, kwargs=None, name='userprofile'),
url(r'^(?P<user_name>[a-zAZ]+)/interests/$',views.interest,name='interests'),
]
i have in my urls.py
url(r'^(?P<slug>.+)/$', page, name='page'),
url(r'^(?P<slug1>.+)/(?P<slug2>.+)/$', subpage, name='subpage'),
page and subpage are two functions in two different models.
and in my app1/views.py
def page(request, slug):
try :
#some code
except myModel.DoesNotExist:
return HttpResponseNotFound('<h1>not found</h1>')
and imy app2/views.py
def page(request, slug1, slug2):
try :
#some code
except myModel.DoesNotExist:
return HttpResponseNotFound('<h1>not found</h1>')
the problem is i dont get the subpage!
if i change the urls to
url(r'^AAAA(?P<slug>.+)/$', page, name='page'),
url(r'^BBBB(?P<slug1>.+)/(?P<slug2>.+)/$', subpage, name='subpage'),
everything goes well !
how can i solve that ?
Don't use . + in your regular expressions. It will match all characters, including slashes. Usually, you would use [-\w]+, which matches letters a-z and A-Z, digits 0-9, hyphens and underscores.
url(r'^(?P<slug>[-\w]+)/$', page, name='page'),
url(r'^(?P<slug1>[-\w]) /(?P<slug2>[-\w]+)/$', subpage, name='subpage'),
I am trying to pass an ID of a table to my function but I am not sure what's going on.
if I hard code the ID number does work, if I use the (?Pd+) with d+ so it use as many digits, like in the tutorials. doesn't work. should this be different?
thanks guys.
my urls
from django.conf.urls import patterns, include, url
from polls import views
urlpatterns = patterns('',
#url(r'^main_site/$', views.main_site),
url(r'^vote/$', views.vote),
url(r'^stadistics/$', views.stadistics),
# using it like this doesn't work
url(r'^vote/Restaurant_Info/(?P<rest_id>d+)/$', views.restaurant_menu),
#testing the info of the restaurant
# hard coding the id of the restaurant does work
url(r'^vote/Restaurant_Info/4/$', views.restaurant_menu),
my view
def restaurant_menu(request, rest_id="0"):
response = HttpResponse()
try:
p = Restaurant.objects.get(id=rest_id)
response.write("<html><body>")
response.write("<p>name of the restaurant</p>")
response.write(p.name)
response.write("</body></html>")
except Restaurant.DoesNotExist:
response.write("restaurant not found")
return response
You're missing a backslash in your expression, currently d+ matches the character d literally "one or more" times. The backslash in combination with a literal character creates a regular expression token with special meaning.
Therefore, \d+ will match digits 0 to 9 "one or more" times.
url(r'^vote/Restaurant_Info/(?P<rest_id>\d+)/$', views.restaurant_menu)
You're missing a slash. It should be (?P<rest_id>\d+)
url(r"^vote/Restaurant_Info/(?P<rest_id>\d+)/$", views.restaurant_menu),
So I have this URL scheme:
(r'^test/(?P<name>\d+)/', 'test'),
def test(request, name):
html = "it worked"
return HttpResponse(html)
however, when I go to the following URL, I get a 404 error:
http://127.0.0.1:8000/test/words/
What am I doing wrong?
You probably meant to use \w instead, e.g.:
(r'^test/(?P<name>\w+)/', 'test'),
\d matches only digits; \w matches any alphanumeric character.
Python Regular Expression HOWTO by A.M. Kuchling.
Im trying to work out a url that will match domain.com\about-us\ & domain.com\home\
I have a url regex:
^(?P<page>\w+)/$
but it won't match the url with the - in it.
I've tried
^(?P<page>\.)/$
^(?P<page>\*)/$
but nothing seems to work.
Try:
^(?P<page>[-\w]+)/$
[-\w] will accept a-z 1-9 and dash