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

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

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 = form.save(commit=False) obj.created_by = self.request.user obj.save() 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

Django Pass Multiple Models to one Template

I ended up modifying @thikonom ‘s answer to use class-based views: class IndexView(ListView): context_object_name=”home_list” template_name=”contacts/index.html” queryset = Individual.objects.all() def get_context_data(self, **kwargs): context = super(IndexView, self).get_context_data(**kwargs) context[‘roles’] = Role.objects.all() context[‘venue_list’] = Venue.objects.all() context[‘festival_list’] = Festival.objects.all() # And so on for more models return context and in my urls.py url(r’^$’, IndexView.as_view(), name=”home_list” ),

Default image for ImageField in Django’s ORM

I haven’t tried this, but I’m relatively sure you can just set it as a default in your field. pic = models.ImageField(upload_to=’blah’, default=”path/to/my/default/image.jpg”) EDIT: Stupid StackOverflow won’t let me comment on other people’s answers, but that old snippet is not what you want. I highly recommend django-imagekit because it does tons of great image resizing … Read more

Error “You’re accessing the development server over HTTPS, but it only supports HTTP”

I think you should create different settings.py ( base_settings.py, local_settings.py, production_settings.py). And in your settings.py do something like this: import socket if socket.gethostname()==”Raouf-PC”: from local_settings import * Change ‘Raouf-PC’ to the hostname of your PC. P:S: I’m using Windows 10. After doing that place the below data in your production_settings.py and save. Then clear your … Read more

context in nested serializers django rest framework

Ok i found a working solution. I replaced the ChildSerializer assignment in the Parent class with a SerializerMethodField which adds the context. This is then passed to the get_fields method in my CustomModelSerializer: class ChildSerializer(CustomModelSerializer): class Meta: fields = (‘c_name’, ) model = Child class ParentSerializer(CustomModelSerializer): child = serializers.SerializerMethodField(‘get_child_serializer’) class Meta: model = Parent fields … Read more

Can Django automatically create a related one-to-one model?

Take a look at the AutoOneToOneField in django-annoying. From the docs: from annoying.fields import AutoOneToOneField class MyProfile(models.Model): user = AutoOneToOneField(User, primary_key=True) home_page = models.URLField(max_length=255) icq = models.CharField(max_length=255) (django-annoying is a great little library that includes gems like the render_to decorator and the get_object_or_None and get_config functions)

tech