Recieving string at Views from Urls - python

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.

Related

django url.py error page not found

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.

Regex to capture url until a certain character

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 '??'

variables with space in url (django)

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/.

How to parse a list in Django urlparser?

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.

Django - trailing slash resets page title

I apologise for the blatant ignorance of this question but I've been charged with fixing something in Django that I have NO experience with!
We're getting an issue with URLs and duplicated content.
If we visit "www.hello.com/services/" then we get our full page rendered, absolutely fine.
If we visit "www.hello.com/services" then we get the same content but with a default that seems to be set in a line:
class PageTitleNode(template.Node):?
?
def render(self, context):?
try:?
meta_info = MetaInfo.objects.get(url=context['request'].path)?
except ObjectDoesNotExist:?
return u'This is our default page title'?
return u"%s - hello.com" % meta_info.title
The main problem with this is that Google is indexing two almost identical pages and it's bad SEO according to our client's overpaid online strategy partner.
I know it's vague but if anyone can help then much rejoicing will be had.
Thanks for reading!
I think your consultant is correct. One URL = one resource. Having two urls on one resource is quite dirty anyway. This is why Django features automatic redirect from non trailing slash to urls with trailing slashes. Under certain conditions.
I'm pretty sure your url definition regexp for /services/ lacks the trailing slash. Anyway, you should use trailing slashes only:
Ensure APPEND_SLASH is set to True: from django.conf import settings; print settings.APPEND_SLASH
Ensure that all your url regexps have the trailing slash, e.g. url(r'foo' ...) is bad, and url(r'foo/' ...) passes barely because of possible conflicts and url(r'foo/$' ...) is better
Ensure all MetaInfo objects have url with trailing slash, e.g. MetaInfo.objects.exclude(url__endswith='/') should return MetaInfo without trailing slash in url

Categories

Resources