Created
July 4, 2013 03:54
-
-
Save eternicode/5924803 to your computer and use it in GitHub Desktop.
Adding a custom view to the admin -- from a project written in Django 1.1.2, meaning don't use this! Please ignore the codesmells :3 Relevant docs:
https://docs.djangoproject.com/en/stable/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_urls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# project/app/admin/__init__.py | |
from admin import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# project/app/admin/admin.py | |
from django.contrib import admin | |
from django.conf.urls.defaults import * | |
from project.app.models import Signup | |
from views import download_list | |
class SignupAdmin(admin.ModelAdmin): | |
def get_urls(self): | |
urls = super(SignupAdmin, self).get_urls() | |
my_urls = patterns('', | |
(r'^download/$', self.admin_site.admin_view(self._download)), | |
) | |
return my_urls + urls | |
def _download(self, request): | |
return download_list(request, self) | |
admin.site.register(Signup, SignupAdmin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# project/app/admin/views.py | |
from django.http import HttpResponse | |
from project.app.models import Signup | |
import csv | |
from StringIO import StringIO | |
def download_list(request, model_admin): | |
data = StringIO() | |
writer = csv.writer(data) | |
list = Signup.objects.values_list('name', 'email') | |
for item in list: | |
writer.writerow(item) | |
data.seek(0) | |
resp = HttpResponse(data.read()) | |
resp._headers['content_disposition'] = ("Content-Disposition", 'attachment; filename="list.csv') | |
return resp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment