How to set up custom middleware in Django

First: The path structure If you don’t have it you need to create the middleware folder within your app following the structure: yourproject/yourapp/middleware The folder middleware should be placed in the same folder as settings.py, urls, templates… Important: Don’t forget to create the init.py empty file inside the middleware folder so your app recognizes this … 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

Accepting email address as username in Django

For anyone else wanting to do this, I’d recommend taking a look at django-email-as-username which is a pretty comprehensive solution, that includes patching up the admin and the createsuperuser management commands, amongst other bits and pieces. Edit: As of Django 1.5 onwards you should consider using a custom user model instead of django-email-as-username.

Django multi-select widget?

From the docs: The Django Admin application defines a number of customized widgets for calendars, filtered selections, and so on. These widgets define media requirements, and the Django Admin uses the custom widgets in place of the Django defaults. The Admin templates will only include those media files that are required to render the widgets … Read more

What’s the cleanest, simplest-to-get running datepicker in Django?

Following is what I do, no external dependencies at all.: models.py: from django.db import models class Promise(models): title = models.CharField(max_length=300) description = models.TextField(blank=True) made_on = models.DateField() forms.py: from django import forms from django.forms import ModelForm from .models import Promise class DateInput(forms.DateInput): input_type=”date” class PromiseForm(ModelForm): class Meta: model = Promise fields = [‘title’, ‘description’, ‘made_on’] widgets … Read more

Access django models inside of Scrapy

If anyone else is having the same problem, this is how I solved it. I added this to my scrapy settings.py file: def setup_django_env(path): import imp, os from django.core.management import setup_environ f, filename, desc = imp.find_module(‘settings’, [path]) project = imp.load_module(‘settings’, f, filename, desc) setup_environ(project) setup_django_env(‘/path/to/django/project/’) Note: the path above is to your django project folder, … Read more