I can't make url.py. It still shows page not found.
These are my urls:
/pins/tag/?search=A/
/pins/tag/?search=B/
/pins/tag/?search=C/
A,B,C are variable strings.
This is my urls.py:
url(r'^pins/tag/?search=(?P<tag>(\w|-)+)/$')
How do I make a correct urls.py?
Regex like this would match anything except newline character (.*). So your URL should look something like this. If you just want Uppercase alphabets stick to [A-Z]
url(r'^pins/tag/?search=(?P<tag>.+)/$', views.search_tag , name='search_tag'),
views.py
def search_tag(request, tag):
#do whatever here with tag
I mapping the url like this in my projects
url(r'^pins/tags/(?P[\w.#+-]+)/$', 'what_app_search', name='search')
Give me a feel back if it resolve the mapping.
Related
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.
With a url such as
https://search.yahoo.com/search?p=Fetty+Wap&fr=fp-tts&
I am using
pat = re.compile('<a href="(https?://.*?)".*',re.DOTALL)
as a search pattern.
I want to pick any url like the yahoo url above, but I want to capture the url up to the literal ? in the actual url.
In other words I want to extract the url up to ?, knowing that all the urls I'm parsing don't have the ? character. In such a case I need to capture all of the url.
The above regex works and extracts the url but goes to the end of the url. How can I get it to stop at the first ? it encounters, and keep going to the end if it doesn't encounter a ?
Regex is really the wrong tool for the job. Doing a basic string split will get you exactly what you want.
def beforeQuestionMrk(inputStr):
return inputStr.split("?")[0]
url = "https://search.yahoo.com/sometext"
url2 = "https://search.yahoo.com/search?p=Fetty+Wap&fr=fp-tts&"
print(beforeQuestionMrk(url))
print(beforeQuestionMrk(url2))
#https://search.yahoo.com/sometext
#https://search.yahoo.com/search
If you really wanted wanted to use regex I suppose you could fo the following:
import re
def getBeforeQuestRegex(inputStr):
return re.search(r"(.+?\?|.+)", inputStr).group(0)
print(getBeforeQuestRegex("https://search.yahoo.com/search?p=Fetty+Wap&fr=fp-tts&"))
print(getBeforeQuestRegex("https://search.yahoo.com/sometext"))
#https://search.yahoo.com/search?
#https://search.yahoo.com/sometext
Bobble bubbles solution above worked very well for me;
"You can try like this by use of negated class: ]*?href="(http[^"?]+)"<- bobbles answer.
url looks like this
https://search.yahoo.com/search?p=Justin+Bieber&fr=fp-tts&fr2=p:fp,m:tn,ct:all......
or it could be something like this
https://www.yahoo.com/style/5-joyful-bob-ross-tees-202237009.html
objective was to extract full url if there was no literal ? in it, but if it did to stop just before the literal ?.
was Bobble Bubbles answer and works very cleanly, does what I wanted done, Again thank you for everyone in participating in this discussion, really appreciate it.
I agree with other answer, that using regexp here is not a solution, especially because there my be any number of parameters before opening of the <a> tag and href parameter, there can be a new line in between too.
but, answering to the initial question:
'*', '+', and '?' qualifiers are all greedy - they match as much text as possible
that's why there are non-greedy versions of them:
'*?', '+?' and '??'
I am having the same issue as How to pass variables with spaces through URL in :Django. I have tried the solutions mentioned but everything is returning as "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."
I am trying to pass a file name example : new 3
in urls.py:
url(r'^file_up/delete_file/(?P<oname>[0-9A-Za-z\ ]+)/$', 'app.views.delete_file' , name='delete_file'),
in views.py:
def delete_file(request,fname):
return render_to_response(
'app/submission_error.html',
{'fname':fname,
},
context_instance=RequestContext(request)
)
url : demo.net/file_up/delete_file/new%25203/
Thanks for the help
Thinking this over; are you stuck with having to use spaces? If not, I think you may find your patterns (and variables) easier to work with. A dash or underscore, or even a forward slash will look cleaner, and more predictable.
I also found this: https://stackoverflow.com/a/497972/352452 which cites:
The space character is unsafe because significant spaces may disappear and insignificant spaces may be introduced when URLs are transcribed or typeset or subjected to the treatment of word-processing programs.
You may also be able to capture your space with a literal %20. Not sure. Just leaving some thoughts here that come to mind.
demo.net/file_up/delete_file/new%25203/
This URL is double-encoded. The space is first encoded to %20, then the % character is encoded to %25. Django only decodes the URL once, so the decoded url is /file_up/delete_file/new%203/. Your pattern does not match the literal %20.
If you want to stick to spaces instead of a different delimiter, you should find the source of that URL and make sure it is only encoded once: demo.net/file_up/delete_file/new%203/.
All I want to do is pass the 'koala 2-2' string from the url to the views. currently it returns a blank string in sitename.
The URL:
http://127.0.0.1:8000/site/koala 2-2
urls.py:
url(r'^site/(?P<sitename>)', site),
views.py
def site(request, sitename'):
return HttpResponse('sitename: {}'.format(request))
You have to use (...) to capture what you want. In your case, use (.+) to capture everything.
There is also an error in your url : It can't have space
Then use :
http://127.0.0.1:8000/site/koala-2-2
and capture koala-2-2 with :
url(r'^site/(?P<sitename>(.+)', site),
If you want something more specific, (E.g: allow only word character and -, then use this one :
url(r'^site/(?P<sitename>([\w-]+)', site),
Thank you for the help guys, below is the final working solution:
urls.py:
url(r'^site/(?P<sitename>(.+))', site),
views.py:
def site(request, sitename):
return HttpResponse('sitename: {}'.format(sitename))
It doesn't seem to care at all about a space being there, it returns the string properly either way.
I searched for an answer to this for about 3 hours before I posted, https://docs.djangoproject.com/en/1.8/topics/http/urls/ has the answer there, but I missed it several times. I sure wish they would use less complex examples.
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.