This question already has answers here:
How to identify an anchor in a url in Django?
(3 answers)
Closed 6 years ago.
I've got a url set up like in python django 1.9
url(r'^faq/?$', views.faq, name="faq"),
However, if I go to a url with #anchors in them, it keeps removing the #anchor part in all browsers.
So, localhost:5000/faq#12 always goes to localhost:5000/faq.
How do I get django to keep the #anchor section?
UPDATE:
I'm not trying to pass any data to the server. The FAQ page has a bunch of questions with unique id. /faq#12 should take the view directly to the div#12. It's for the browser and doesn't have anything to do with the server side at all.
Anchor part of the url not sent to the server. it only used at client side.
Your url config defines a slash at the end, so you have to use it in the URL as well:
http://localhost:5000/faq/#12
If you miss it, there will be a redirect that removes the anchor.
Related
This question already has answers here:
Sending data from HTML form to a Python script in Flask
(2 answers)
Closed 3 months ago.
I have a very simple python code that retrieves the share price of some stocks. I have managed to include that in Django/Flask so I can see this in a html page. I would like to create an html button that when I click on it it runs the python script and the share prices refresh (and remain on the same html page e.g. index.html) however I can't make it work. Can you please help with the html code for the button and the piece of code that I need to add to the app to make it work?
If you have written a class, or a method, then you can just call it an store the returned value in a variable and pass it to the html file using jinja2.
would be something like:
return render_template("index.html", parameter1 = returned_value)
and in your html file:
<p>{{ parameter1 }}</p>
Take a look:
https://hackersandslackers.com/flask-jinja-templates/
The only way to do what you want is by a remote call to the server. But by no mean you can refresh your list entirely client-side, especially with Python only.
What you must do if you really want not to reload the page is to :
client-side : make a request to the server from a Javascript function then fetch the response and refresh your share prices the incoming data
server-side : create a new view that will not render a template then return an html response, but serialize your data and return a JSON response.
That's all I can say to you. Do not expect the Stackoverflow community to give you a ready-to-use answer before you have brought a proof that you tried by yourself.
This question already has answers here:
Can't retrieve variable from url with Flask app after submitting search form
(2 answers)
Create dynamic arguments for url_for in Flask
(2 answers)
Closed 4 years ago.
I have a search bar on my Flask powered website which works well when the user clicks the search button (I am using a JS code with a POST request). However it doesn't work when the user sends the form with an 'enter' click. In those cases the browser loads a link like this /?finder=searchedword.
The finder is the value of the name attribute of the html code used for the form. In order to solve the problem, I have tried to setup a redirect that always redirects to the proper link that should be loaded, but unfortunately using this solution doesn't work. It just simply redirects to the main page "/".
This is how I manage the redirect:
#app.route("/?finder=<tag>")
def search(tag):
t = tag.replace(' ', '-')
return redirect("/result/%s/" % (t,), code=301)
My question is what would be the proper technique to handle this type of redirection? This kind of redirection always works for me and I am using it in several apps, therefore I have no idea what is the weak part of the implementation.
Past the question mark is not actually part of the route but rather is arguments to the page. So Flask is just matching the url "/".
Try something like this:
#app.route("/")
def search():
if "finder" in request.args:
t = request.args["finder"].replace(" ", "-")
return redirect("/result/%s/" % (t,), code=301)
This question already has answers here:
Programmatic Python Browser with JavaScript
(8 answers)
Closed 4 years ago.
I am new to this subject, so my question could prove stupid.. sorry in advance.
My challenge is to do web-scraping, say for this page: link (google)
I try to web-scrape it using Python,
My problem is that once I use Python requests.get, I don't seem to get the full content of the page. I guess it is because that page has many resources, and Python does not get them all. (more than that, once I scroll my mouse up - more data is reviled on Chrome. I can see from the source code that no more data is downloaded to be shown..)
How can I get the full content of a web page? what am I missing?
thanks
requests.get will get you the page web but only what the page decides to give a robot. If you want the full page web as you see it as a human you need to trick it by changing your headers. If you need to scroll or click on buttons in order to see the whole page web, which is what I think you'll need to do, I suggest you take a look at selenium.
This question already has answers here:
Scrape a dynamic website [duplicate]
(8 answers)
Closed 6 months ago.
I need to scrape news announcements from this website, Link.
The announcements seem to be generated dynamically. They dont appear in the source. I usually use mechanize but I assume it wouldnt work. What can I do for this? I'm ok with python or perl.
If the content is generated dynamically, you can use Windmill or Seleninum to drive the browser and get the data once it's been rendered.
You can find an example here.
The polite option would be to ask the owners of the site if they have an API which allows you access to their news stories.
The less polite option would be to trace the HTTP transactions that take place while the page is loading and work out which one is the AJAX call which pulls in the data.
Looks like it's this one. But it looks like it might contain session data, so I don't know how long it will continue to work for.
There's also WWW::Scripter "For scripting web sites that have scripts" . Never used it.
In python you can use urllib and urllib2 to connect to a website and collect data. For example:
from urllib2 import urlopen
myUrl = "http://www.marketvectorsindices.com/#!News/List"
inStream = urlopen(myUrl)
instream.read(1024) # etc, in a while loop
# all your fun page parsing code (perhaps: import from xml.dom.minidom import parse)
This question already has answers here:
How can I get all the request headers in Django?
(10 answers)
Closed 6 years ago.
I've been looking over what I can find about this and found something about denying access to specific user-agents but couldn't find how I can actually get the full request header. I am trying to make a customized analytics app so would like access to the full headers.. any info is appreciated.
All the headers are available in request.META. See the documentation.