我正在尝试让自定义视图在 Django 管理员中工作。我有下一个模型:
class Reseller(models.Model):
first_name = models.CharField(max_length=64, verbose_name='First Name')
last_name = models.CharField(max_length=64, verbose_name='Last Name')
email = models.CharField(max_length=64, verbose_name='E-mail')
password = models.CharField(max_length=64, blank=True, editable=False)
这就是我添加自定义按钮(重置密码)的方式。自定义视图(扩展 change_form.html)我有这个:
{% extends "admin/change_form.html" %}
{% load i18n %}
{% block object-tools %}
{% if change %}
<ul class="object-tools">
<li><a href="reset_password/">Reset Password</a></li>
</ul>
{% endif %}
{% endblock %}
这就是我所拥有的admin.py
from django.conf.urls.defaults import patterns
from django.contrib import admin
from django.shortcuts import redirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from myapp.resellers.models import Reseller
class ResellerAdmin(admin.ModelAdmin):
list_display = ('id', 'first_name', 'last_name', 'email')
list_filter = ('email')
search_fields = ('first_name', 'last_name', 'email')
ordering = ['-id', ]
def get_urls(self):
urls = super(ResellerAdmin, self).get_urls()
my_urls = patterns('',
(r'(?P<id>\d+)/reset_password/$',
self.admin_site.admin_view(self.reset_password)),
)
return my_urls + urls
def reset_password(self, request, id):
entry = Reseller.objects.get(pk=id)
[...GENERATE AND SEND PASSWORD FUNCTION GOES HERE...]
return redirect(entry)
admin.site.register(Reseller, ResellerAdmin)
当我运行此代码时,我得到下一个:argument of type 'Reseller' is not iterable
. 我是 Django 的新手,所以基本上某处有一个愚蠢的错误,所以请不要投反对票:)