我正在尝试在Django admin中使用自定义视图。我有下一个型号:
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的新手所以基本上某处有一个愚蠢的错误,所以请不要downvote:)
答案 0 :(得分:2)
如果您只将模型作为参数传递给redirect
,那么将调用模型get_absolute_url()方法,您可能没有定义(参见docs)。
因此,请继续向您的转销商类添加get_absolute_url()
方法。