I'm trying to get custom view working in Django admin. I have the next model:
我正在尝试在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)
This is how I added a custom button (reset password). Custom view (extends change_form.html) I have for this:
这就是我如何添加自定义按钮(重置密码)。自定义视图(扩展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 %}
That's what I have in admin.py
这是我在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)
When I run this code I get the next: argument of type 'Reseller' is not iterable
. I'm new to Django so basically there's a stupid mistake somewhere, so please don't downvote :)
当我运行这段代码时,我得到了下一个:类型为'Reseller'的参数不可迭代。我是Django的新手,所以这里有一个愚蠢的错误,所以请不要投反对票。
1 个解决方案
#1
2
If you only pass the model as an argument to redirect
then the models get_absolute_url() method will be called, which you presumably have not defined (see docs).
So go ahead and add a get_absolute_url()
method to your Reseller class.
如果只将模型作为参数传递给重定向,那么将调用模型get_absolute_url()方法,您可能还没有定义这个方法(请参阅文档)。因此,继续向您的经销商类添加get_absolute_url()方法。
#1
2
If you only pass the model as an argument to redirect
then the models get_absolute_url() method will be called, which you presumably have not defined (see docs).
So go ahead and add a get_absolute_url()
method to your Reseller class.
如果只将模型作为参数传递给重定向,那么将调用模型get_absolute_url()方法,您可能还没有定义这个方法(请参阅文档)。因此,继续向您的经销商类添加get_absolute_url()方法。