Login

Snippets by bowdengm

Snippet List

RegEx redirect fallback middleware

Simple middleware to complement the built in redirect middleware app. Add this after the contrib.redirect middleware - this will be fired if a 404 is triggered and the contrib.redirect fails to find a suitable redirect. Useful if you want to add the redirects into the DB - and/or don't have access to the .htaccess script or whatever HTTP server based redirect machinery your site runs off. You simply add in regex 'old_path' and 'new_path' entries into the same redirect app, but this will try to do a regex find and replace - i.e. >>> r = new Redirect() >>> r.old_path = '/my/test/path/([a-z]+)/with/regex/' >>> r.new_path = '/my/result/path/$1/with/regex/' >>> r.save() this will convert: /my/test/path/abcdef/with/regex/ into: /my/result/path/abcdef/with/regex/' also works with query strings: /index.php?section=products >>> old_path = '/index.php/\?section=([a-z]+)' #need to add in the forward slash if ADD_SLASHES = True in your settings, and escape the question mark. >>> new_path = '/section/$1/' converts the url to '/section/products/'

  • regex
  • Redirect
  • /
Read More

head inclusion middleware

Inspired by [http://www.djangosnippets.org/snippets/712/](YUI Loader as Django middleware) This loads in css and javascript files where you want them (usually in the head) - but allows you to put them anywhere in your code - i.e. in a TemplateTag. so your head code will look like this: ` <head> ... <!-- HEAD_init --> ... </head> ` then somewhere in your templates you can load in javascript or css files like so: ` <!-- HEAD_include myfile.js myotherfile.css --> ` It automatically checks if you've already included the files, and only puts them in once. It automatically figures out if its a javascript or css file by the file name - If you have an irregular filename (i.e. a google maps api script url) you can force it by using either of the following tags: ` <!-- HEAD_include_js [my javascript file] --> <!-- HEAD_include_css [my css file] --> ` Or you can write inline code to get rendered in the head: ` <!-- HEAD_render <script> someJavascriptCall(); </script> --> ` Todo: make it compress the js into one file...

  • middleware
  • javascript
  • css
Read More

bowdengm has posted 2 snippets.