Here is my urlpatterns
urlpatterns = [
url(r'^volunteer/$', views.volunteerInformation, name='volunteerInformation'),
url(r'^volunteer/(?P<ID>[0-0]{1})/$', views.volunteerInformation, name='volunteerInformation'),
]
Here is the view that I'm trying to call
def volunteerInformation(request, ID=None):
volunteers = Volunteer.objects.all()
if ID:
print ID
else:
print "XKCD"
return render(request, 'dbaccess/volunteer.html', {'volunteers': volunteers})
When the url is .../volunteer/, it prints XKCD. But when the url is ..../volunteer/1, I'm getting an error that the page was not found. Here is the error:
^ ^volunteer/(?P<ID>[0-0]{1})/$ [name='indVolunteerInformation']
^ ^volunteer/$ [name='volunteerInformation']
^admin/
The current URL, volunteer/3, didn't match any of these.
What can I do about it?
Your url regex is wrong, you are searching for numbers of length 1 in the range 0-0. To match any number change this:
^volunteer/(?P<ID>[0-0]{1})/$
for something like
^volunteer/(?P<ID>\d+)/$
Related
I am trying to redirect to a function from another function in views.
But I am getting following error
NoReverseMatch at /sigma/status1/
Reverse for 'testview' with keyword arguments '{'amount': 1.000, 'stat':'Approved', 'ref': '10917'}' not found. 1 pattern(s) tried: ['sigma\\/status2/(?P<amount>\\d+)/(?P<stat>[a-z][A-Z]+)/(?P<ref>\\d+)/$']
Below is corresponding part of my views.py
return redirect(reverse('testview',kwargs={'amount':1.000,'stat':'Approved','ref':str(res['ref'])}))
def payment_status2(request,amount,stat,ref):
return render(request, 'confirm1.html')
Below is corresponding part of my urls.py
urlpatterns = [
url('status1/', views.payment_status1),
url(r'^status2/(?P<amount>\d+)/(?P<stat>[a-z][A-Z]+)/(?P<ref>\d+)/$', views.payment_status2,name="testview"),
]
Your stat regex is incorrect:
?P<stat>[a-z][A-Z]+
This indicates that the argument should start with a lowercase letter and then followed by one or more uppercase letters (such as aPPROVED). You should change it to:
?P<stat>[a-zA-Z]+
or
?P<stat>\w+
I get this error message
Using the URLconf defined in esarcrm.urls, Django tried these URL patterns, in this order:
1. ^person/ duplicate_check/(?P<entity>)/(?P<full_name>)/?$
2. ^admin/
3. ^api/v1/
4. ^api/v1/authenticate/$ [name='api_authenticate']
5. ^static\/(?P<path>.*)$
6. ^media\/(?P<path>.*)$
The current path, person/duplicate_check/candidate/tom, didn't match any of these.
Please not the space here 1. ^person/[SPACE]duplicate_check
my project/urls.py
urlpatterns = [
url(r'^person/', include('person.urls')),
url(r'^admin/', admin.site.urls),
url(r'^api/v1/', include(router.urls)),
url(r'^api/v1/authenticate/$', crm_views.ApiAuthenticateView.as_view(), name='api_authenticate'),
]
my app.urls
urlpatterns = [
url(r'duplicate_check/(?P<entity>)/(?P<full_name>)/?$', views.check_if_exist),
]
my app.views
#api_view(['GET'])
def check_if_exist(request, entity, first_name):
if entity == 'candidate':
candidates = person_models.Candidate.objects.filter(first_name=first_name)
serializer = person_serializers.CandidateMiniSerializer(candidates, many=True)
return Response(serializer.data)
What exactly am I missing?
There is no space, that's just how Django prints the URLs.
The problem has nothing to do with spaces, but with your URL. "duplicate_check" is included under person/, but you are trying to access p_check/....
Edit There are actually bigger problems with your URL pattern. You haven't actually given the capturing groups anything to capture. You need some kind of pattern inside the parentheses. Something like:
r'^duplicate_check/(?P<entity>\w+)/(?P<full_name>\w+)/?$'
which will capture all alphanumeric characters for entity and full_name.
I am trying to implement an approve and decline functionality of a profile using the following approach:
/user/area/decline/234322
/user/area/approve/234322
So I wrote the following URL pattern:
urlpatterns = i18n_patterns(
url(r'^user/area/decline/(?P<userid>\[0-9]+)/$', views.DeclineUser),
url(r'^user/area/approve/(?P<userid>\[0-9]+)/$', views.ApproveUser),
url(r'^user/area/$', views.Index),
.
.)
And my views:
#login_required
def DeclineUser(request, userid=""):
print("Here: " + userid)
#login_required
def ApproveUser(request, userid=""):
print("Here: " + userid)
But something is wrong and the methods are not triggered and the Index is triggered instead so I guess the problem is that the URL RegEx is not matching what I need.
Both of your url's have a \ in them so you are escaping the opening [ bracket, you just need to remove those slashes.
url(r'^user/area/decline/(?P<userid>[0-9]+)/$', views.DeclineUser),
url(r'^user/area/approve/(?P<userid>[0-9]+)/$', views.ApproveUser),
I have a weird problem can't figure out whats wrong, I get 404 errors for any url that contains a '-' character...
my urls.py in the project root works fine
url(r'^serialy/', include('movieSite.series.urls')),
next, is
urlpatterns = patterns('',
url(r'^$', views.serialy_index, name='serialy_index'), #this works
(r'^(?P<serial_title>[\w]+)/$', serial),
)
The second one, using serial_title, works only if the series' title is something like, 'Dexter,' or 'Continuum.' But other series have titles like 'Family guy,' so when I create the url I use a function that changes it to 'Family-guy,' but for some reason it won't work for those titles with the '-' characters. I always get a 404 error like this
Using the URLconf defined in movieSite.urls, Django tried these URL patterns, in this order:
^serialy/ ^$ [name='serialy_index']
^serialy/ ^(?P<serial_title>[\w]+)/$
^static\/(?P<path>.*)$
The current URL, serialy/Whats-with-Andy/, didn't match any of these.
so here the url serialy/whats-with-andy/ doesn't match, but if I visit serialy/continuum it works fine?? anyone have any ideas as to what could cause this?
Oh and this is what the view looks like
def strip(s):
s.replace('-',' ')
return s
def serial(request, serial_title=None) :
s_title = strip(serial_title)
obj = Show.objects.get(title__icontains=s_title)
#episodes = obj.episodes
des = obj.description
img = obj.image
title = obj.title
t = get_template('serial.html')
html = t.render(Context({
'the_title':title,'the_image':img,'the_description':des
}))
return HttpResponse(html)
The regex [\w]+ only matches words and not special chars like -.
If you change it to [-\w]+ it'll match "slug" urls.
I think your regex is failing because '-' is not considered a match for \w. See here:
https://docs.python.org/2/library/re.html
I'm trying to get my head around regexp in Django urls. I'm currently developing locally and I want to be able to direct a request such as http://localhost:8000/options/items/item-string-1121/ to the 'details' view in my app called 'options', passing in the final number part of the request string (1121) as a parameter 'id' to the view function.
The signature for details in options/views.py is as follows, taking id=1 as default:
def details(request, id=1):
...
I have the following in my root urls.py:
...
urlpatterns += patterns('',
url(r'^options/, include(options.urls')),
)
and in options/urls.py:
urlpatterns = patterns('options.views',
url(r'^items/(.+)(P<id>\d+)/$', 'details'),
...
)
Now when I try to request the above URL the dev server says it tried to match against the pattern ^options/ ^items/(.+)(P<id>\d+)/$ but it doesn't match.
Can anyone see the problem?
You need a non-greedy quantifier on the (.+), so r'^items/(.+?)(P\d+)/$'. Otherwise that first glob happily eats until the end of the string, preventing the ID from ever matching.
You are missing quotes.
urlpatterns += patterns('',
url(r'^options/, include(options.urls')),
)
Should be
urlpatterns += patterns('',
url(r'^options/', include('options.urls')),
)
I'm not too sure of your expression, might try this:
urlpatterns = patterns('options.views',
url(r'^items/(?<=-)(?P<id>\d+)/$', 'details'),
...
)