Login

Tag "rails"

Snippet List

Rails-like environments using Django

This is a replacement for settings.py, which moves the actual settings files into a directory called "env" and establishes different versions for different settings of the environment variable DJANGO_ENV. At runtime, the specified development environment can be found and loaded into the local context of settings.py, which is then picked up by whatever routine manage.py is kicking off.

  • django
  • rails
  • environment
Read More

Rails-like MVC Controllers for Django

[See the blog entry](http://sciyoshi.com/blog/2008/nov/18/rails-mvc-controllers-django/) Allows using controllers for views. This allows for nice subclassing to override behavior of views. `Controller.urls` (see below) works fine for subclasses as well. Similar to [snippet #1165](http://www.djangosnippets.org/snippets/1165/) except that it won't break reverse URL resolving and regex validation in URLs. In `views.py`: import mvc class MyController(mvc.Controller): @mvc.view('myview/$', 'myview') def my_view(self): # do something with self.request return HttpResponse('something') class Meta(mvc.Controller.Meta): url_prefix = 'mycontroller-' In `urls.py`: from . import views urlpatterns = patterns('', # ... other urls here ... *views.MyController.urls(r'prefix/') ) Then the view `MyController.my_view` will be accessible from `'prefix/myview/'` and have the name `'mycontroller-myview'`, i.e. `reverse('mycontroller-myview')` will work as expected.

  • rails
  • controller
  • mvc
Read More

Cookie based flash errors and notices (a la Rails)

This is a light-weight flash implementation. Instead of hitting the database it uses cookies. The messages are shown to the user only once, after that the cookies are deleted. I tested it on Google App Engine, but it should work on vanilla Django as well, there's no GAE specific code. To set up, add `"path.to.flash.Middleware"` to the `MIDDLEWARE_CLASSES` list. Also add `'path.to.flash.context_processor'` to the `TEMPLATE_CONTEXT_PROCESSORS` list. In your views, import and call `flash_error(msg)` and `flash_notice(msg)` passing the message that you want to show. In your base template use this mark up: {% if flash.notice %} <div id="flash_notice"> <p>{{ flash.notice }}</p> </div> {% endif %} {% if flash.error %} <div id="flash_error"> <p>{{ flash.error }}</p> </div> {% endif %} And finally, add this to your CSS file changing colours as necessary: #flash_error { margin-top: 30px; padding: 20px; background-color: #FFCCCC; border: solid 1px #CC0000; } #flash_notice { margin-top: 30px; padding: 20px; background-color: #CCFFCC; border: solid 1px #00CC00; } #flash_error p, #flash_notice p { margin: 0px; } Please comment if you notice any FUs. I'm new to Django and will appreciate any feedback.

  • error
  • flash
  • rails
  • notification
  • notice
Read More

Extended rails flash implementation

This is an extendend version of the Rails Flash implementation by Sean Patrick Hogan that supports different message types. **Setting a flash message:** request.flash.error = 'Item could not be saved' request.flash['error'] = 'Item could not be saved' request.flash['foo'] = 'bar' **Displaying a flash in the view:** <!-- show the error message --> {% if flash.error %}An error occured:{{ flash.error }}{% endif %} <!-- just show the first message found --> {% if flash %}An error occured:{{ flash }}{% endif %} <!-- show all messages --> {% for msg in flash %}{{ msg.type }}: {{ msg.msg }}{% endfor %} Note that it still works with simple strings as well. Feel free to just use it like this: request.flash = "Message" And: {% if flash %}{{ flash }}{% endif %} However, be aware that once you did this, you destroyed the Flash() dict and thus lost the extended functionality. You can use request.flash.clear() to remove all messages.

  • flash
  • messages
  • rails
Read More

4 snippets posted so far.