I'm looking to integrate some API's through a web application interface, to achieve this I need to identify the absolute path for a specific route. As I dont want to hard code any urls I'd like to do this dynamically in the flask app.
I thought that should be simple enough and tried a couple of things:
abs_url = url_for('home')
This didn't work of course as it just returned the route. I since learnt that url_for requires the function name. It then returns the route ('/home' for example).
I then tried:
abs_url = redirect(url_for('home'))
This gave me a redirect object, I was hoping path may be a variable I could call, however that doesnt seem to be the case.
Google has not been my friend today and hasn't given me anything to go on and advice would be appreciated.
The more I think about this the more I think the simplest option would be to capture the application address as part of the configuration.
To resolve this I have captured the application address as part of the initial info gathering form.
Related
I'm making a webapp with flask and SQL.
What I'm trying to do I can't seem to find the correct search terminology for:
I have my homepage at myapp/
I want to make it so when users add whatever path (myapp/whatever; myapp/hello) they will be rendered a certain view/blueprint (not redirected) and the name of the path ('whatever', 'hello') will be newly added to my database (unless someone has already opened it before)
I am sure this is a very common thing to do but I just cannot seem to find maybe the correct way to phrase it for google. I kind of found something about request.path which will give me the 'whatever'/'hello' part but I can't figure out how to piece the rest together.
Therefore, if someone could please either direct me with what to search, or if you know how to do it or have any resources, I will be very grateful. Thank you :) :)
What I understand what you're looking for is a route that accepts a keyword argument, creates a record in the database, and returns a template.
#app.route('/<path_name>')
def save_path():
# Do database insert here
return render_template('mytemplate.html')
I have been researching this for quite a while and it seems that I cannot get my head around it. I have different ideas but all of them looks like they are not the right one to solve this problem.
I have an application in flask that does different things like for example:
loading an image (user profile image)
calling APIs (to perform specific tasks, like for example adding a comment to a forum post)
etc.
The way I am doing all of this is by using url_for in this way:
client_info = requests.post(url_for('api.read_comments', _external=True),
json={'client_name': client}).json()
So my application is at "https://www.myapp.com/comments" - The "url_for()" in my code will basically generate this url: "https://www.myapp.com/api/read_comments".
All comments are then retrieved by my API code.
This works just fine, but what I would like to do is to have the Frontend on "https://www.myapp.com/" but the Backend on "https://backend.myapp.com".
So my question is, how can I make my url_for() as dynamic as possible and able to access content on other subdomains/servers that I own?
Any thoughts? Or additional methods/functions that I should be using?
For example all comments will be located at "comments.myapp.com", all my profile details are on "profiles.myapp.com", etc. This is just a general idea on how the system can be setup. Any thoughts?
Thanks a lot and I look forward to hearing from you.
I am new to Django, and i am now trying to use the HttpResponseRedirect() function. But I am so confused that if I use it like HttpResponseRedirect('good/'), and the current page is '/bad/', it can only be redirected to '/bad/good/', which is an url of current page appended with the url value from the HttpResponseRedirect() function. I tried to search google, and could not find any solution.
How can I redirect to the page with specific url? For example, HttpResponseRedirect('/good/') to /good/ rather than /bad/good/ ?
Surely you must see that there's a difference between 'good/' and '/good/'? The former will always add itself onto the existing page, whereas the latter will start from the root. This is basic web behaviour, and nothing to do with Django.
In any case, you should never hard-code URLs like that, but should use Django's URL-reversing functionality to calculate the URLs dynamically.
If you want to redirect to urls in your domain, you can use redirect. You can use redirect to view with by using patterns in your urls.py file or to any urls you 'd like. Although I strongly encourage the usage of views as indicates the different tutorials available on their website.
Better check out django documentation that is one of the most complete out there (to my humble opinion)
/edit for lack of clarity indeed.
I am in the midst of writing a web app in CherryPy. I have set it up so that it uses OpenID auth, and can successfully get user's ID/email address.
I would like to have it set so that whenever a page loads, it checks to see if the user is logged in, and if so displays some information about their login.
As I see it, the basic workflow should be like this:
Is there a userid stored in the current session? If so, we're golden.
If not, does the user have cookies with a userid and login token? If so, process them, invalidate the current token and assign a new one, and add the user information to the session. Once again, we're good.
If neither condition holds, display a "Login" link directing to my OpenID form.
Obviously, I could just include code (or a decorator) in every public page that would handle this. But that seems very... irritating.
I could also set up a default index method in each class, which would do this and then use a (page-by-page) helper method to display the rest of the content. But this seems like a nightmare when it comes to the occasional exposed method other than index.
So, my hope is this: is there a way in CherryPy to set some code to be run whenever a request is received? If so, I could use this to have it set up so that the current session always includes all the information I need.
Alternatively, is it safe to create a wrapper around the cherrypy.expose decorator, so that every exposed page also runs this code?
Or, failing either of those: I'm also open to suggestions of a different workflow. I haven't written this kind of system before, and am always open to advice.
Edit: I have included an answer below on how to accomplish what I want. However, if anybody has any workflow change suggestions, I would love the advice! Thanks all.
Nevermind, folks. Turns out that this isn't so bad to do; it is simply a matter of doing the following:
Write a function that does what I want.
Make the function in to a custom CherryPy Tool, set to the before_handler hook.
Enable that tool globally in my config.
I am having problem to get graphical result for second time either it be for a database query or two or more queries. For first time result is given as desired immediately but thereafter browser just says 'Connecting'. For example after http://localhost:8000/graph/ gives pie-chart for first time it does not give the pie-chart again when the same link is hit and also any other similar link http://localhost:8000/graph2/ doesnot work. The latter link would have worked if http://localhost:8000/graph2/ was hit first after execution of program.
In urls.py links are given as:
url(r'^graph/', graph, name = 'ngraph'),
url(r'^graph2/', graph2, name = 'ngraph2'),
Directory structure looks:
Detail code is given HERE.
Two options:
In your urls.py, add a separate pattern object with the prefix set as the views of your app. That would be like urlpatterns += patterns('welcome.views', url(r'^graph/', ...)). Rest should be as it is.
Dont change urls.py. Instead refer to the complete path in the template starting from your app. In your case that might be {% url welcome.views.ngraph %}.
Since you mentioned that GET is not being served at all, it is quite possible that the server cant locate (map) the url to the function.
In an ideal situation, you should import welcome.views instead of using from .. import * and polluting the urls.py globals. Use the complete path in the patterns object. This way you wont need separate patterns objects for different apps. This would make a lot of sense in a bigger project with more applications. But in any case, this is the standard practice and is encouraged.
Do post your result.