python - Change admin template in Django -
i have next simple model:
class reseller(models.model): type = models.charfield( max_length=32, choices=( ('active', 'active'), ('inactive', 'inactive'))) 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)
i have automatically generated admin section crud model. want add together "resend password" button (which send new credentials reseller) @ update page don't know how. know extends functionality don't know how apply correctly. after clicking page go same page adding kind of nitification 'new password has been sent'.
also don't know how handle new button action within django admin. code snippets useful. thanks
update created change_form.html
, extends original template
{% extends "admin/change_form.html" %} {% load i18n %} {% block object-tools %} {% if alter %} <ul class="object-tools"> <li><a href="reset_password/">reset password</a></li> </ul> {% endif %} {% endblock %}
but don't know how treat http request. how can pass parameters? should create form instead of button?
you might need write admin action, check django documentation more details.
update: should create this, action can applied 1 or more selected resellers resellers list page. end this:
from django.contrib import admin myapp.models import reseller class reselleradmin(admin.modeladmin): ... actions = ['reset_password'] def reset_password(self, request, queryset): obj in queryset: obj.password = genereate_new_passwd() sender.send(obj.email, obj.password) obj.save() self.message_user(request, "passwords resets selected resellers successfully.") reset_password.short_description = "resend passwords selected resellers" admin.site.register(reseller, reselleradmin)
update 2: provide custom action in model alter page, need customize reselleradmin change_view() , provide custom template custom action.
from django.contrib import admin myapp.models import reseller change_form_template = 'admin/myapp/extras/reseller_change_form.html' class reselleradmin(admin.modeladmin): def change_view(self, request, object_id, form_url='', extra_context=none): if(comming new action) # intercept request post here, if comes new button, reset password else: homecoming super(reselleradmin, self).change_view(request, object_id, form_url, extra_context=extra_context)
another approach customize form adding boolean field, , on save_model() check if true, reset password , save, otherwise save object. in case dont have provide custom template.
class reselleradmin(admin.modeladmin): def save_model(self, request, obj, form, change): if alter , form.is_reset: #do reset password , send obj.save() def get_form(self, request, obj=none, **kwargs): form = super(reselleradmin, self).get_form(request, obj, **kwargs) form.fields['is_reset'] = form.booleanfield() homecoming form
the code above not finish show idea, more details on customizing modeladmin can found here
python django
No comments:
Post a Comment