How to set css class of a label in a django form declaration?

Widgets have an attrs keyword argument that take a dict which can define attributes for the input element that it renders. Forms also have some attributes you can define to change how Django displays your form. Take the following example: class MyForm(forms.Form): error_css_class=”error” required_css_class=”required” my_field = forms.CharField(max_length=10, widget=forms.TextInput(attrs={‘id’: ‘my_field’, ‘class’: ‘my_class’})) This works on any … Read more

datetime and timezone conversion with pytz – mind blowing behaviour

The documentation states “Unfortunately using the tzinfo argument of the standard datetime constructors ‘does not work’ with pytz for many timezones.” The code: t = datetime( 2013, 5, 11, hour=11, minute=0, tzinfo=pytz.timezone(‘Europe/Warsaw’) ) doesn’t work according to this, instead you should use the localize method: t = pytz.timezone(‘Europe/Warsaw’).localize( datetime(2013, 5, 11, hour=11, minute=0))

Migrating existing auth.User data to new Django 1.5 custom user model?

South is more than able to do this migration for you, but you need to be smart and do it in stages. Here’s the step-by-step guide: (This guide presupposed you subclass AbstractUser, not AbstractBaseUser) Before making the switch, make sure that south support is enabled in the application that contains your custom user model (for … Read more

How to use custom AdminSite class?

The Problem Using a custom class derived from django.contrib.admin.AdminSite for the admin site of a project, without having to write custom registration code to register models with the new class. When I use 3rd party apps with their own models, I’d rather not have to edit custom registration code only because models were added or … Read more

Accessing request.user in class based generic view CreateView in order to set FK field in Django

How about overriding form_valid which does the form saving? Save it yourself, do whatever you want to it, then do the redirect. class PlaceFormView(CreateView): form_class = PlaceForm @method_decorator(login_required) def dispatch(self, *args, **kwargs): return super(PlaceFormView, self).dispatch(*args, **kwargs) def form_valid(self, form): obj = obj.created_by = self.request.user return http.HttpResponseRedirect(self.get_success_url())

UnicodeEncodeError: ‘ascii’ codec can’t encode character

For anyone encountering this problem when running Django with Supervisor, the solution is to add e.g. the following to the supervisord section of Supervisor’s configuration: environment=LANG=”en_US.utf8″, LC_ALL=”en_US.UTF-8″, LC_LANG=”en_US.UTF-8″ This solved the problem for me in Supervisor 3.0a8 running on Debian Squeeze. Also make sure Supervisor re-reads the configuration by running: supervisorctl reread supervisorctl restart myservice … Read more

What is the best way to access stored procedures in Django’s ORM

We ( / wrote that django snippet but its not the whole story (actually that code was only tested on Oracle at that time). Stored procedures make sense when you want to reuse tried and tested SP code or where one SP call will be faster than multiple calls to the database – or … Read more