Getting a URL in python2.7 - python

In a Djano project, I need to get the complete URL of my web app in a view, along with the URL's anchor tag. For instance, if the URL is:
http://example.com/#section0
I need to get that in its entirely, including the #section0 part. As you'd already know, HTTP_REFERER doesn't suffice (which is what I've tried).
What's a good way to read URLs in python/django? Beginner here, please advise.

Sorry, but the anchor tag is never sent to the server. The only way a server could know what it was is if the server itself sent the user to a url with the anchor tag.
See here for more information:
Is the anchor part of a URL being sent to a web server?

Related

Get anchor tag from Django request URL

I have a Django template filter where I'm trying to check the request URL to see whether it includes an anchor tag or not.
For example, I want to check if the request URL is:
/mypath/mypage/my-slug/ or /mypath/mypage/my-slug/#myanchor
If it is the latter, I want to get the anchor tag ID.
Currently, the anchor tag seems to be stripped from the request information: request.path, request.build_absolute_uri() and so on. I can only get /mypath/mypage/my-slug/.
The URL pattern for the page in question is:
re_path(r'^(?P<product_slug>[\w-]*)_(?P<pk>\d+)/$', self.detail_view.as_view(), name='detail')
I can see the regex looks for the line end after the last slash, which suggests the anchor is being excluded, but I can easily retrieve GET params from the URL and I expected this to be straightforward too. It feels like I am missing something pretty obvious.
The section tag is not sent to the server, but interpreted by the browser :
Is the anchor part of a URL being sent to a web server?
Now, if you need to the anchor ID, you will have to do so using AJAX and write your own view to receive the data.

URL configuration in Django

I have a Django project in which the HTML page has a simple GitHub link.
Github
When I click on it, the URL it gets redirected to is "localhost/app/github.com"
Can you please explain why is it happening and what should I do to correct it?
This has nothing to do with Django, but is standard HTML.
You need to put the full URL, including protocol:
Github

How do I make Python urlib2 to cleverly avoid the security check while trying to log into a site?

I am trying to crawl a website for the first time. I am using urllib2 Python
I am currently trying to log into Foursquare social networking site using Python urlib2 and Beautifulsoup. To view a particular page, I need to provide username and password.
So,I followed the Basic Authentication described on the ducumentation page.
I guess, everything worked well, but the site throws up a security check asking me to type a text (capcha), before sending me the required page. It obviously looks like, the site is detecting that, a page is being requested not by a human, but a crawler.
So, what is the way, to avoid being detected. How to make urllib2 get the desired page, without having to stop at the security check? Pls help..
You probably want to use foursquare API instead.
You have to use the foursquare API. I guess, there is no other way. API are designed for such purposes.
Crawlers depending solely on the HTML format of the page will fail in the furture when the HTML page changes

Python/Django: How to Prepend a # on to all URLS

I am building a mobile web app with Django and jQuery Mobile. My problem is that jQuery Mobile likes for all links to be prepended with a # so it can accurately keep track of browsing history.
Example: http://www.fest.com/#/foo/1/
I would like know how to automatically redirect all urls that point From: /foo/1/ To: /#/foo/1/
If I don't do that and someone goes directly to /foo/1/, then clicks a link pointing to /bar/2/, they'll end up with a URL path like this:
/foo/1/#/bar/2/
I would very much like to prevent that from happening because its causes lots of problems. Whats the best way to do this?
You have misunderstood what the # does.
The # in a URL is the "fragment" separator. Nothing after that is sent to the server. So there is no such URL as "foo. com#/foo" - as far as the server is concerned, it's just "foo.com". So you can't do any server-side redirection.
If your JS library is using the fragments to simulate navigation, you'll need to handle this with Javascript.
This is jquery mobile, so the answer is a bit different. Jquery mobile uses #something for history when working with AJAX. The AJAX call is introduced for every <a href=...
So you just link to a page like this: <a href="some.html?var1=foo" and JQM calls an ajax on it without reloading the page AND stores the item in the DOM document to not load again. The url is updated to have #some.html at the end and it's how the history is managed.
<a href="#something" WILL NOT work as in a normal page, because jquery mobile takes over.
Read here to get all info on links in jquery mobile: http://jquerymobile.com/demos/1.0a2/#docs/pages/link-formats.html

is Video tag in html a POST request or GET request?

I am iusing in my html. I am trying to handle the request on server side using python BaseHTTPServer. I want to figure out how the request from video tag looks like???
It will be a simple GET request, just like any other resource embedded in an HTML document.
If you really want to examine exactly what browsers send, then use something like Charles or the Net tab of Firebug.
POST is usually reserved for form submissions because you are POSTing form information to the server. In this case you are just GETing the contents of a <video> source.

Categories