Django urlencode template filter - django

I'm attempting to URL encode a string that contains slashes in Django 1.3 using the optional argument shown in the docs:
{{ someString|urlencode:"" }}
However, the slashes aren't getting URL encoded, they're left intact. So, if someString is "A/V Equipment", I'm getting "A/V%20Equipment". What am I doing wrong?

"/" is already a valid url character and as such will not be encoded to something else. You are not doing anything wrong. I take it you are having issues with some sort of clean url argument that gets confused with the / character? If this is the case, rather pass the string in as a query parameter.

Related

How do I use APEX_UTIL.PREPARE_URL on a URL that contains backslashes?

I'm trying to use APEX_UTIL.PREPARE_URL to produce an APEX URL for a page that requires a checksum, and I'm trying to set a page item on the destination page to a value that contains special characters. Assume for this question that the only special characters that I'm passing are commas, though my problem seems to apply to all special characters.
According to the APEX URL documentation, an item value that contains a comma needs to be enclosed by backslashes. So when I build my URL, I end up with something like this:
f?p=112:50:session::::P50_PAGE_ITEM:\123,abc\:
Then I pass this URL through APEX_UTIL.PREPARE_URL:
APEX_UTIL.PREPARE_URL('f?p=112:50:session::::P50_PAGE_ITEM:\123,abc\:')
When I try to follow the link that's returned, I get the following error:
The checksum computed on the request, clear cache, argument names, and argument values (...) did not match the checksum passed into the show procedure.
If I try passing a value that does not contain commas and I remove the backslashes, the value is passed without a problem.
How can I get a valid checksum for a URL that does contain the backslashes so that I can pass values that contain commas (and other special characters)?
APEX_UTIL.PREPARE_URL should handle backslashes fine. The region that contains the resulting URL may be escaping the special characters, causing the checksum mismatch.
To test the actual output of APEX_UTIL.PREPARE_URL, try outputting your URL in a PL/SQL Dynamic region. For example:
DECLARE
l_url VARCHAR2(200) := 'f?p=112:50:session::::P50_PAGE_ITEM:\123,abc\:';
BEGIN
htp.p('page 52');
END;

How to pass parameter values from Postman which has '#' in it?

I am trying to call a webservice from Postman client and while doing this I am passing a value which has '#' in it. Example:
test = "43543#324#435"
But when I enter this value and click somewhere else it will remove all characters from first '#' including '#'. So the parameter will become:
test = "43543"
What should I do so that I can pass a parameter with '#' in it?
Note: 'Postman' is a Google Chrome add-in to test Rest webservices.
You should not use # in your URL parameters.
You can find out why in this answer here: Characters allowed in GET parameter
Not use # in URL so use encode of # is %23 and replace #.
for example show in below.
test = "43543%23324%23435"
I got the solution.
As some special characters are not allowed in URL, so we need to encode them. For this, in bulk edit mode for key-value pair, select value which contains special characters and right click. Select 'EncodeURIComponenet' and send request. It will work :)

How to query by string

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.

Ignore backslash in django url patterns

In my django project, I have a url pattern like
(r'^survey/u2=([^/]+)/u3=([^/]+)/$',SurveyView.as_view()).
When I try to open the below url
http://www.sample.com/survey/u2=rc57S4/jyTJBz+==/u3=/U5pKfrV8X1MjUU2tI0AhqTF5PGR8g=/
[where u2 & u3 are encrypted value using internal keys. ]
I'm getting page not found error. This is due to, the sample url is not matching with the original url pattern at server end, as it has '/' backslash character in the url parameter.
Right now,I'm not in a position to edit the sample url by adding encode to the parameters, since this url has been mailed to customer. However if the customer opens the link I should not through error message.
How can I handle this special characters at server end while pattern match for url?.
Instead of passing as arguments in URL pass it as a GET request. seperated by ? and & characters.

How do I match the question mark character in a Django URL?

In my Django application, I have a URL I would like to match which looks a little like this:
/mydjangoapp/?parameter1=hello&parameter2=world
The problem here is the '?' character being a reserved regex character.
I have tried a number of ways to match this... This was my first attempt:
(r'^pbanalytics/log/\?parameter1=(?P<parameter1>[\w0-9-]+)&parameter2=(?P<parameter2>[\w0-9-]+), 'mydjangoapp.myFunction')
This was my second attempt:
(r'^pbanalytics/log/\\?parameter1=(?P<parameter1>[\w0-9-]+)&parameter2=(?P<parameter2>[\w0-9-]+), 'mydjangoapp.myFunction')
but still no luck!
Does anyone know how I might match a '?' exactly in a Django URL?
Don't. You shouldn't match query string with URL Dispatcher.
You can access all values using request.GET dictionary.
urls
(r'^pbanalytics/log/$', 'mydjangoapp.myFunction')
function
def myFunction(request)
param1 = request.GET.get('param1')
Django's URL patterns only match the path component of a URL. You're trying to match on the querystring as well, this is why you're having trouble. Your first regex does what you wanted, except that you should only ever be matching the path component.
In your view you can access the querystring via request.GET
The ? character is a reserved symbol in regex, yes. Your first attempt looks like proper escaping of it.
However, ? in a URL is also the end of the path and the beginning of the query part (like this: protocol://host/path/?query#hash.
Django's URL dispatcher doesn't let you dispatch URLs based on the query part, AFAIK.
My suggestion would be writing a django view that does the dispatching based on the request.GET parameter to your view function.
The way to do what the original question was i.e. catch-all in URL dispatch var...
url(r'^mens/(?P<pl_slug>.+)/$', 'main.views.mens',),
or
url(r'^mens/(?P<pl_slug>\?+)/$', 'main.views.mens',),
As far as why this is needed, GET URL's don't exactly provide good "permalinks" or good presentation in general for customers and to clients.
Clients often times request the url be formatted i.e.
www.example-clothing-site.com/mens/tops/shirts/t-shirts/Big_Brown_Shirt3XL
this is a far more readable interface for the end-user and provides a better overall presentation for the client.