I am receiving this error in one of my templates and cant seem to figure out what's wrong.
`NoReverseMatch: Reverse for 'getimagefile'
with arguments '(12L, 'afN9LRzESh4I9CGe6tFVoA==\n')' and
keyword arguments '{}' not found.
My urls.py contains:
urlpatterns = patterns('myproj.myapp.views',
url(r'^getimage/(?P<extractedcontent_id>\d+)/(?P<encpw>.*)/$','getimagecontent',name='getimagefile'),
)
My views.py contains:
def getimagecontent(request,extractedcontent_id,encpw):
........
And finally my template that's giving me the error contains the following line:
<li class="active"><img src="{% url getimagefile img,encpw %}" title=""/></li>
Your encpw variable ends in a newline character, by default the . regular expression character does not capture these. Try altering your regex so the DOTALL flag is turned on, which will match for newline characters.
url(r'(?s)^getimage/(?P<extractedcontent_id>\d+)/(?P<encpw>.*)/$','getimagecontent',name='getimagefile'),
Notice the (?s) at the very beginning this will turn the DOTALL flag on.
You don't show where encpw comes from, but it appears to have a newline character (\n) at the end, which won't match the url regex.
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 the following urlpatterns:
url(r'^api/daily-means/$', views.daily_means.as_view(), name='daily_means'),
url(r'^api/daily-means/sites/(?P<url>\w+)/$', views.site_daily_means.as_view()),
url(r'^api/daily-means/pollutant/(?P<poll>\w+)$/', views.pollutant_daily_means.as_view()),
The first two work fine. The last one show work the same as the second one but it does not. Im not that great with regex and urlpatterns but I assume there is something with the second url pattern which is stopping the last one from running. Can anyone else see a reason for this?
Django will append the end slash if it is not provided. In your regex, you are matching without the end slash.
url(r'^api/daily-means/pollutant/(?P<poll>\w+)$/', views.pollutant_daily_means.as_view()),
The following URL pattern should work(after including the end slash as a part of URL match).
url(r'^api/daily-means/pollutant/(?P<poll>\w+)/$', views.pollutant_daily_means.as_view()),
In Product model and i want to query by food title. and return me NoReverseMatch error
html url:
some text
views.py:
def product(request, food_name):
product = Catalog.objects.get(food_name=food_name)
return render(request, 'food/product.html', {'product':product})
url.py
url(r'^product/(?P<food_name>\w+)/', food_views.product, name='product'),
Trace
NoReverseMatch: Reverse for 'product' with arguments '()' and keyword arguments '{u'food_name': u'%D9%86%D8%A7%D9%86%20%D8%A8%D9%88%D8%B1%DA%A9'}' not found. 1 pattern(s) tried: [u'product/(?P<food_name>\\w+)/']
Remove the urlencode, you don't need it
some text
urlencode is used when you need to encode a string in a way that will allow it to be used inside a url (such as when you're adding get parameters). Above, you are just passing a string parameter to a function that is constructing a url.
You seem to be trying to encode arabic characters into your url which are not matched by \w so you need to update your url to support these
^product/(?P<food_name>[\w\u0600-\u06FF]+)/
Will handle most of these (See this regexr example), but I'm not familiar with arabic enough to know what the unicode for ک is
I believe it's because \w+ doesn't match URL-encoded string. Try to change it temporarily to .* (just to check if there are not any other issues). If it will work — change \w+ to better template matching URL-encoded strings.
I have declared an url like following in django 1.7:
url(r'^page(/\w{2}/|/)$', MyView.as_view(), name='my_name'),
In my template, I want to reverse the url from its name. I tried:
<form method="post" action="{% url 'my_namespace:my_name' variable %}">
<form method="post" action="{% url 'my_namespace:my_name' %}">
But nothing works, it throwed exception:
Reverse for 'my_name' with arguments '(u'test',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['page(\/\w{2}\/|\/)$']
Please help by giving me some advice. Thank you.
The regex appears incorrect for matching the argument 'test'.
If we take a look at the regex (/\w{2}/|/),
It appears to be capturing two groups which are separated by an 'OR' (|) operator.
The first group is /\w{2}/. This is only going to match '/' followed by \w only 2 times followed by '/'.
The second group is only matching '/'.
'test' doesn't match either one of these cases so it raises an exception.
I believe the correct regex we are looking to capture here would be,
(\w+)
The correct URL structure would look like this,
url(r'^page/(\w+)/$', MyView.as_view(), name='my_name')
url(r'^page/$', MyView.as_view(), name='my_name')
This would match given the argument 'test' because the regex (\w+) says match any of the characters in this group [a-zA-Z0-9_] one or more times and each character in 'test' falls into this category.
All of the examples I can find of urlpatterns for django sites have a separate entry for incoming urls that have no leading slash, or the root folder. Then they handle subfolders on each individual line. I don't understand why a simple
/?
regular expression doesn't permit these to be on one simple line.
Consider the following, let's call the Django project Baloney and the App name is Cheese. So in the project urls.py we have something like this to allow the apps urls.py to handle it's requests...
urlpatterns = patterns('',
(r'^cheese/', include('Baloney.Cheese.urls')),
)
then inside of the Cheese apps urls.py, I don't understand why this one simple line would not trigger as true for all incoming url subpaths, including a blank value...
urlpatterns = patterns('',
(r'^(?P<reqPath>.*)/?$', views.cheeseapp_views),
)
Instead, it matches the blank case, but not the case of a value present. So...
http://baloneysite.com/cheese/ --> MATCHES THE PATTERN
http://baloneysite.com/cheese/swiss --> DOES NOT MATCH
Basically I want to capture the reqPath variable to include whatever is there (even blank or '') but not including any trailing slash if there is one.
The urls are dynamic slugs pulled from the DB so I do all the matching up to content in my views and just need the url patterns to forward the values along. I know that the following works, but don't understand why this can't all be placed on one line with the /? regular expression before the ending $ sign.
(r'^$', views.cheeseapp_views, {'reqPath':''}),
(r'^(?P<reqPath>.*)/$', views.cheeseapp_views),
Appreciate any insights.
I just tried a similar sample and it worked as you wrote it. No need for /?, .* would match that anyway. What is the exact error you are getting? Maybe you have your view without the request parameter? I.e. views.cheeseapp_views should be something like:
def cheeseapp_views(request, reqPath):
...
Edit:
The pattern that you suggested catches the trailing slash into reqPath because * operator is greedy (take a look at docs.python.org/library/re.html). Try this instead:
(r'^(?P<reqPath>.*?)/?$', views.cheeseapp_views)
note it's .*? instead of .* to make it non-greedy.