URL dispatching in web.py - python

In Django whenever I have to use site navigation I use the Reverse resolution of URLs. This way I can make django -render- each new html page and pass that page whatever arguments I want to through the views.
However I am wondering how should I do this in web.py. For instance, I have a web.py template that contains a variable $user. At some point in the main webpage a simple button contains a link of the form
Account
which redirects a user to his account page. Now, I need to pass $user on to account.html so that he/she can change his/her details. The problem is that since I can't directly link to account.html cause it's not a static page, how should I go through web.py and use its render method?
Thanks in advance.

It seems to me that the best way to do this would be with a session variable. That kind of variable can be consistent across several pages.

Related

Wagtail - extending PageChooserBlock to support external URLs

I'm building a site with Wagtail and using StreamField to build up content on a homepage. I've built a block that allows users to add featured links, which could be internal or external links. Currently the featured links have both a PageChooserBlock and a URLBlock, but I'd like to add a new custom block type that allows a user to specify either an internal page or a URL. I can't see anything in the docs that would help me. Any ideas where to start?
This is nothing Wagtail supports yet, but there's a lot of interest in this feature, see issue https://github.com/wagtail/wagtail/issues/3141.
To solve this there is a pull request work in progress (see https://github.com/wagtail/wagtail/pull/1645) that aims to unify link choosers.
Maybe you are able to contribute, I'm sure this would be very welcome!
you can use this library: https://pypi.org/project/wagtail-link-block/
From the docs:
"A link block to use as part of other StructBlocks which lets the user choose a link either to a Page, Document, or external URL, and whether or not they want the link to open in a new window."

Django Contact form and sending confirmation on page

I got one page. On the page is a contact form. After clicking the 'send' button I want to change the form containing div to 'thank you for mailing us' div. And stay at the same 'id' on the page.
One way to solve it is to put an another template and load it as 'success url' But this way the whole page is being loaded again and the user lands on top of the page without seeing the confirmation.
Is there a way not to load the page again and to change the div somehow? With jQuery for example? How to do it? Is there even an another way?
Would be great to have some hints. Many thanks in advance!
Using Javascript is the easiest solution. Here is example for using AJAX with forms. Django can detect AJAX requests out of the box via request.is_ajax().
Another option would be to set cookie, redirect to the same url and handle the response differently (depending on given cookie), but that isn't elegant at all.

With bottle.py, how do I properly render static file requests of templates?

I am experimenting with bottle.py, and I'm coming across some problems with requesting static templates vs rendering them.
I have some basic routes:
#route("/feed")
def show_feed():
# query database, calculate things, etc.
# code to show feed (which is dynamic)
#route("/submit")
def show_submit():
# query database, calculate things, etc.
# code to show submit
#route("/<filename:path>")
def serve_static(filename):
# code to simply return static files
I also have some templates:
views/submit.html
views/feed.html
There are no problems with the web server itself. It works as it is told. The problem is when I use links in my templates, as usual, to go from page to page:
Go back to the feed
When a user clicks on that link or manually enters ".../feed.html" or ".../submit.html", the URL .../feed.html is requested instead of /feed, and Bottle routes that to the serve_static(filename) function. As a result, the template is not rendered - instead, the static template is returned, complete with ugly things like "{{article[0]}}" and "% end % end".
How can I get Bottle to render these templates properly?
Is there a way for Bottle to know when to render templates when they are requested as static files? Is it considered an okay practice to change href attributes to what the server should expect? Is there something I'm not considering?
The problem is very simply that
<a href="feed.html">
has a wrong href given your URL pattern. Just change it to
<a href="feed">
It's also a good idea, as BrenBarn suggested, to move the templates into their own separate subdirectories, away from the one from which you want to serve truly static public files. However, per se, that wouldn't solve your problem -- you'd just get a 404 on clicks on the link in question. The core issue is fixing that <a>'s href!

Pass data in google app engine using POST

I'm trying to pass a variable from one page to another using google app engine, I know how to pass it using GET put putting it in the URL. But I would like to keep the URL clean, and I might need to pass a larger amount of data, so how can pass info using post.
To illustrate, I have a page with a series of links, each goes to /viewTaskGroup.html, and I want to pass the name of the group I want to view based on which link they click (so I can search and get it back and display it), but I'd rather not use GET if possible.
I didn't think any code is required, but if you need any I'm happy to provide any needed.
Links inherently generate GET requests. If you want to generate a POST request, you'd need to either:
Use a form with method="POST" and submit it, or
Use AJAX to load the new page.

Pylons: Routes information availability from templates

I'm building a Pylons application using evoque as our templating engine, though I think my question is relevant to other template engines. I have a base template that I'm using for our pages, and that base template does all the includes for CSS and Javascript files. I'd like to perform conditional test to include/exclude CSS and Javascript files based on the actual page being display. Is there a way to access the routes information from the template, in other words to get the /{controller}/{action} information? This would allow me to get only the relevant CSS and Javascript files for that page based on the controller/action combination.
Thanks in advance,
Doug
You can pull the controller and action information from environ['pylons.routes_dict']['controller'] and ['action'].
I'm not sure if environ is passed into the tmpl_context by default, but if not, you can just add something like this to the BaseController.__before__ method:
c.routes_dict = environ['pylons.routes_dict']
Then reference c.routes_dict['controller'] in your template.

Categories

Resources