Pylons: Routes information availability from templates - python

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.

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."

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!

URL dispatching in web.py

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.

Python : Can I do front end of django project using HTML / CSS?

Hi I am totally new to web development frameworks .I am currently learning python and django from the Django project tutorial..
My (rather dumb) question is when I am done with the small "poll" website will I be able to modify the front end of this project using HTML/CSS ?
Also, should I use tools like Dreamweaver to do that ? or are there other ways ?
Of course you will be able. That is the point of the web frameworks to let you build the whole application including frontend.
You'll get it all once you finish the tutorial. You'll see that Django has very nice templating language that allows you to build a html page according to data you get from your application.
<h1>{{ section.title }}</h1>
This result in normal HTML page in the end. And of course you can write any kind of CSS for those pages to get whatever look for your page you like.
Yes you can, you'll need to edit the templates in the location specified by TEMPLATE_DIRS. The templates are just html mixed with some python code to fill the content.

How can I use css files in django?

I'm using a template with block tags to include a css file.
Unsure on how to manage css files, I put this in my urls.py:
url(r'style.css$','portal.views.css')
with this in my views
def css(request):
return render_to_response('style.css')
When I use chrome's "inspect element" on any page that uses the template I can see all the css in the file, however nothing at works on the page itself. To test it I added
body
{
display:none;
}
But still no changes.
Do I need to render the response in a different way?
You shouldn't serve static files (like css files) that way.
Django provides a specific solution for that, make sure you read the complete and comprehensive documentation on that.

Categories

Resources