Change Django ModelChoiceField to show users’ full names rather than usernames

You can setup a custom ModelChoiceField that will return whatever label you’d like. Place something like this within a fields.py or wherever applicable. class UserModelChoiceField(ModelChoiceField): def label_from_instance(self, obj): return obj.get_full_name() Then when creating your form, simply use that field UserModelChoiceField(queryset=User.objects.filter(is_staff=False), required = False) More info can be found here

Django’s self.client.login(…) does not work in unit tests

The code that doesn’t work: from django.contrib.auth.models import User from django.test import Client user = User.objects.create(username=”testuser”, password=’12345′) c = Client() logged_in = c.login(username=”testuser”, password=’12345′) Why doesn’t it work? In the snippet above, when the `User` is created the actual password hash is set to be `12345`. When the client calls the `login` method, the value … Read more

How to deploy an HTTPS-only site, with Django/nginx?

For the 2nd part of John C’s answer, and Django 1.4+… Instead of extending HttpResponseRedirect, you can change the request.scheme to https. Because Django is behind Nginx’s reverse proxy, it doesn’t know the original request was secure. In your Django settings, set the SECURE_PROXY_SSL_HEADER setting: SECURE_PROXY_SSL_HEADER = (‘HTTP_X_FORWARDED_PROTO’, ‘https’) Then, you need Nginx to set … Read more

Putting a django login form on every page

Ok, I eventually found a way of doing this, although I’m sure there are better ways. I created a new middleware class called LoginFormMiddleware. In the process_request method, handle the form more or less the way the auth login view does: class LoginFormMiddleware(object): def process_request(self, request): # if the top login form has been posted … Read more

Enforcing password strength requirements with django.contrib.auth.views.password_change

I also went with a custom form for this. In urls.py specify your custom form: (r’^change_password/$’, ‘django.contrib.auth.views.password_change’, {‘password_change_form’: ValidatingPasswordChangeForm}), Inherit from PasswordChangeForm and implement validation: from django import forms from django.contrib import auth class ValidatingPasswordChangeForm(auth.forms.PasswordChangeForm): MIN_LENGTH = 8 def clean_new_password1(self): password1 = self.cleaned_data.get(‘new_password1’) # At least MIN_LENGTH long if len(password1) < self.MIN_LENGTH: raise forms.ValidationError(“The new … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)