Django URL does not see the link and its POST request - python

I made a project, but in it you need to get a special token from the VK social network. I made the token pass along with the link. She looks like this:
http://127.0.0.1:8000/vk/auth#access_token=7138dcd74f5da5e557943b955bbfbd9a62811da7874067e5fa0edef1ca8680216755be16&expires_in=86400&user_id=397697636
But the problem is that the django cannot see this link. I tried to look at it in a post request, get request, but everything is empty there. I tried to make it come not as a request but as a link, it is like this:
http://127.0.0.1:8000/vk/auth #access_token=7138dcd74f5da5e557943b955bbfbd9a62811da7874067e5fa0edef1ca8680216755be16&expires_in=86400&user_id=397697636
But the django does not want to read the space. Who can help

I think there is a confusion between a query string (get params) that follows a ? and a fragment (the text, that follows a #)
What follows the # is not sent to the server (and thus not received by Django) it is only useful to the web browser and to the javascript that is executed on the browser , which can use it to update parts of the screen. use it as virtual urls / bookmarks for one page web applications.
The javascript can of course also trigger AJAX requests using that data, but that's up to the javascript
If you write however http://127.0.0.1:8000/vk/auth?access_token=7138dcd74f5da5e557943b955bbfbd9a62811da7874067e5fa0edef1ca8680216755be16&expires_in=86400&user_id=397697636 (you replace # with ?)
Then you can receive the information in your django view with
request.GET["access_token"], request.GET["expires_in"] and request.GET["user_id"]
If it is really a #, then your javascript should parse whatever follows the # and make the according AJAX requests to the server to send / validate the token.
For another question about fragments, refer for example to Is the URL fragment identifier sent to the server?

Related

URL masking in Python Django

I have built a website using Django framework(www.example.com). While navigating the site the URL changes to like (www.example.com/home or /profile etc).
Is there some way that the current url is masked by a placeholder eg(www.example.com/home should be shown as www.example.com).
This should work throughout the website.
The url shown to the user would remain same (www.example.com) to where ever the user navigates on the site
HIGHLY NOT RECOMMENDED
Keep a single link in your urls.py, containing r'^$'.
Handle all requests purely as POST request.
Note: You'll have to hard code every parameter, and detect manually from where the request is coming.
To know how to work with POST request in detail check Django docs

How can I scrape information from HowLongToBeat.com? It doesn't use a variable in the URL

I'm trying to scrape information from How Long to Beat, how can I make a request for a search without having to put the search-term in the URL?
EDIT for clarity:
The problem I face is that the site doesn't use something like http://www.howlongtobeat.com/search.php?s=search-term, therefore I cannot do something like
url = 'http://www.howlongtobeat.com/search.php?s='
search_term = raw_input("Search: ")
r = requests.get(url + search_term)
In other words, when you type the search-term in the search dialog, the site doesn't refresh nor show a change in the URL so I can't find a way to search from outside the site.
I'm sorry if I made grammar mistakes, english is not my first language.
This is because the page is driven by AJAX requests - it updates automatically without redirecting you to visible URL.
If you open developer tools in your browser (F12) and navigate to Network panel, you will see that there are indeed requests sent to the server. I typed "test2" and got following:
As you see, request is sent to a URL that looks like this: http://www.howlongtobeat.com/search_main.php?t=games&page=1&sorthead=popular&sortd=Normal%20Order&plat=&detail=0.
I typed "test2", but it's nowhere to be seen.
That's because it was sent using POST request, e.g. the parameters were embedded in the HTTP request itself, not the URL. When I navigated to "Params" tab in the Developer Tools, indeed I could see my input:
queryString: "test2"
So in order to use this search form, you should send a POST request to that URL containing variable "queryString" filled with whatever value you need.
I strongly encourage asking the site owners' about an API, though. Using publicly available form engines that are designed to be used by end-users in automated fashion is considered unethical.

Python - reading the returnURL

I'm using python without a server to deploy to. Im trying to test the accept payment flow for Paypal.
In the code after sending a POST request I store the result into a local file and then open this file using "webbrowser".
However I suspect that I am missing something here since once I log in as a user I'm not automatically redirected to authorize the transaction but rather just log in.
Now I suspect this is because once I hit the Payment API endpoint it is redirecting me to the login API with some parameters
http://www.paypal.com?hypotheticalredirecturl=etc
Now I am capturing the response i.e - the html of the paypal login page. However the whole
URL cannot be captured. Hence the part
hypotheticalredirecturl=etc
cannot be captured and I think that this is stopping the flow from going to the
authorization page after I log in as a user.
I think if I appended the "hypotheticalredirect" part to my webpage after opening it using "webbrowser" I might be able make the flow normal.
Does any one know of any way to capture the url of the response?
I tried looking into the page itself but I dont think its there.
Any help will be appreciated.
Thanks,
Ashwin
EDIT : Using urllib and urllib2. Should I be looking at httplib?

Pass Data From Python To Html <p> Tag

I am developing a project on Python using Django. The project is doing lot of work in the background so i want to notify users what's going on now in the system. For this i have declared a p tag in HTML and i want to send data to it.
I know i can do this by templates but i am little confused as 5 functions need to pass the status to the p tag and if i use render_to_response() it refreshes the page every time a status is passed from the function
Anyone please tell me how to do this in the correct way
Part of your page that contains the paragraph tags is a piece of JavaScript that contains a timer.
Every once in a while it does an Ajax request to get the data with regard to "what's going on now in the system".
If you use the Ajax facilites of JQuery, which is probably the easiest, you can pass a JavaScript callback function that will be called if the request is answered. This callback function receives the data served by Django as response to the asynchroneous request. In the body of this callback you put the code to fill your paragraph.
Django doesn't have to "know" about Ajax, it just serves the required info from a different URL than your original page containing the paragraph tags. That URL is part the your Ajax request from the client.
So it's the client that takes the initiative. Ain't no such thing as server push (fortunately).

How to submit a request by POST in Pyramid?

In Pyramid, send a request by GET can be done by create URL like this:
#view_config(route_name="test")
def test(request):
return HTTPFound("http://test.com/redirect_to_another_test?a=1")
But it seems that the HTTPFound can't do that by POST, then how can I do that?
Does anyone have ideas about this? Thanks!
You can't do this in Pyramid or any other Server-Side Framework or Language.
Your example code isn't showing a form submission, the code is showing a HTTP redirect. It is instructing the Browser to visit another URL, or in other words, telling the browser to resubmit the request.
This Stack Overflow question discusses this same concept, although in ASP not Python - Response.Redirect with POST instead of Get?
If you were to "submit a request" in Pyramid via GET or POST, you would have to use a library like urllib2, requests, or similar. In those instances, the libraries would have the Pyramid server act as the "submitter" of the request.
If you want to have the User/web-broswer submit the request by POST, you would have to do some fancy footwork/trickery to make the browser do that.
Possible ways to accomplish that would include:
use JavaScript AJAX form submission, and return an error code or instruction via JSON telling your JavaScript library to resubmit the same form via POST.
redirect the user to a GET page which has the form-data filled out (via the GET arguments) , and use javascript to resubmit the form via POST onload
You can't, in any server side language, tell the browser to resubmit a request by POST. Browsers don't work like that.
You should also know that "Most Browsers" will generally interpret any redirect request to be fetched via GET -- even if the original was a POST. That's not to spec - certain redirect codes are supposed to keep a POST a POST - however browsers don't follow all the specs. There is also no HTTP status code or command to purposefully switch from GET to POST .

Categories

Resources