I have a url which contains %20. I want to replace it with - with the help of regex
I already tried replace method
url(r'^timeanalysis/(?P<name>[\w|\W]+)'.replace('%20','-'), timeseries.timeanalysis, name='timeanalysis')
I don't want to change my database.
This is something you need to fix at the point you generate the link, not in the urlconf.
Usually you would use a SlugField that is automatically pre-populated from the title/name, replacing spaces with dashes as necessary.
Related
I have set up my coldfusion application to have dynamic urls on the page, such as
www.musicExplained/index.cfm/artist/:VariableName
However my variable names will sometimes contain slashes, such as
www.musicExplained/index.cfm/artist/GZA/Genius
This is causing a problem, because my application presumes that the slash in the variable name represents a different section of the website, the artists albums. So the URL will fail.
I am wondering if there is anyway to prevent this from happening? Do I need to use a function that replaces slashes in the variable names with another character?
You need to escape the slashes as %2F.
You could easily replace the forward slashes / with something like an underscore _ such as Wikipedia uses for spaces. Replacing special characters with underscores, etc., is common practice.
You need to escape those but don't just replace it by %2F manually. You can use URLEncoder for this.
Eg URLEncoder.encode(url, "UTF-8")
Then you can say
yourUrl = "www.musicExplained/index.cfm/artist/" + URLEncoder.encode(VariableName, "UTF-8")
Check out this w3schools page about "HTML URL Encoding Reference":
https://www.w3schools.com/tags/ref_urlencode.asp
for / you would escape with %2F
My problem is the following:
Inside my urls.py I have defined these url patterns:
url(r'^image/upload', 'main.views.presentations.upload_image'),
url(r'^image/upload-from-url', 'main.views.presentations.upload_image_from_url'),
the problem is when I call from my browser the URL
myowndomain:8000/image/upload-from-url
Django always execute the first pattern (r'^image/upload')
Is there any solution to my problem?
Django uses the first matching pattern, and your ^image/upload pattern doesn't include anything to stop it matching the longer text. The solution is to require that your pattern also match the end of the string:
r'^image/upload$'
By convention, Django URLs generally have a trailing slash as well, but that's not strictly required:
r'^image/upload/$'
You need to insert the dollar sign "$" at the end of the pattern. The dollar sign is a character that represents position. In the case of regex, this is the end of the string. Because both image/upload and image/upload-from-url match what you're looking for, you need to explicitly say where to stop in the pattern.
On Stack Overflow, you can view a list of questions with multiple tags at a URL such as http://stackoverflow.com/questions/tagged/django+python.
I'd like to do something similar in a project I am working on, where one of the url parameters would be a list of tags, but I'm not sure how to write a regex urlparser that can parse it out. I'm fond of SO's way of using the + sign, but it's not a dealbreaker. I also imagine that the urlparser may have to take the whole string (foo+bar+baz) as a single variable to give to the view, which is also fine as I can just split it in the view itself- that is, I'm not expecting the URL parser to give the view an already split list, but if it can, even better!
Right now all I have is:
url(r'^documents/tag/(?P<tag>\w+)/$', ListDocuments.as_view(), name="list_documents"),
Which just pulls out one single tag since \w+ just gets me those [A-Za-z0-9_], but not +. I tried something like:
url(r'^documents/tag/(?P<tag>[\w+\+*])/$', ListDocuments.as_view(), name="list_documents"),
But this no longer matched documents/tag/foo nor documents/tag/foo+bar.
Please assist, I'm not so great with regex, thanks!
It's not possible to do this automatically. From the documentation: "Each captured argument is sent to the view as a plain Python string, regardless of what sort of match the regular expression makes." Splitting it in the view is the way to go.
The second regex in your answer is OK, but it does allow some things you might not want (e.g. 'django+++python+'). A stricter version might be something like: (?P<tag>\w+(?:\+\w+)*). Then you can just do a simple tag.split('+') in the view without worrying about any edge cases.
This works for now:
url(r'^documents/tag/(?P<tag>[A-Za-z0-9_\+]+)/$', ListDocuments.as_view(), name="list_documents"),
But I'd like to be able to get that w back in there instead of the full list of characters like that.
[Edit]
Here we go:
url(r'^documents/tag/(?P<tag>[\w\+]+)/$', ListDocuments.as_view(), name="list_documents"),
I will still select a better answer if there is a way for the Django urlparser to give the view an actual list instead of just one big long string, but if that's not possible, this solution does work.
I have a url like this
test/report
and
test/report/1
and i want to write a matching url regex using django
I tried out this
url(r'^test/report/(0?\d+)/$'
but it did not work . Can any one know how to do this? Thank you in advance.
you just need to create two regexes, one without the slash, and another including the slash:
url(r'^test/report$'
url(r'^test/report/(\d\d?)$
lenik's answer is generally the proper way, but if you must have it in a single regex, you should be able to use this:
url(r'^test/report(/\d\d?)?$')
# Meaning:
# ( Start contents of a node
# / A slash
# \d A digit
# \d? A second digit, or nothing
# )? Finish node. Must find everything in the node, or nothing
Can you please try with url(r'^test/report/\d{1,2}/$' instead ?
I have the following code in Python:
linkHTML = "click here" % strLink
The problem is that when strLink has spaces in it the link shows up as
click here
I can use strLink.replace(" ","+")
But I am sure there are other characters which can cause errors. I tried using
urllib.quote(strLink)
But it doesn't seem to help.
Thanks!
Joel
Make sure you use the urllib.quote_plus(string[, safe]) to replace spaces with plus sign.
urllib.quote_plus(string[, safe])
Like quote(), but also replaces spaces
by plus signs, as required for quoting
HTML form values when building up a
query string to go into a URL. Plus
signs in the original string are
escaped unless they are included in
safe. It also does not have safe
default to '/'.
from http://docs.python.org/library/urllib.html#urllib.quote_plus
Ideally you'd be using the urllib.urlencode function and passing it a sequence of key/value pairs like {["q","with space"],["s","with space & other"]} etc.
As well as quote_plus(*), you also need to HTML-encode any text you output to HTML. Otherwise < and & symbols will be markup, with potential security consequences. (OK, you're not going to get < in a URL, but you definitely are going to get &, so just one parameter name that matches an HTML entity name and your string's messed up.
html= 'click here' % cgi.escape(urllib.quote_plus(q))
*: actually plain old quote is fine too; I don't know what wasn't working for you, but it is a perfectly good way of URL-encoding strings. It converts spaces to %20 which is also valid, and valid in path parts too. quote_plus is optimal for generating query strings, but otherwise, when in doubt, quote is safest.