如何自定义django admin

How to customize django admin

此代码有效 properly.But 我想要某种形式来编写主题和消息以便回复而不是在回复功能中定义主题消息。我如何在 Django 管理页面中为回复操作添加回复表单

admin.py

class ContactAdmin(admin.ModelAdmin):
list_display = ['full_name','email','comment','time']
list_filter = ['time']
search_fields = ['name','comment']
readonly_fields = ['full_name','email','comment','time']
ordering = ['time']
actions = ['reply']

def reply(self,request,queryset):
    for reciever in queryset:
        send_mail('subject','message',settings.EMAIL_HOST_USER,[reciever.email],fail_silently=False)
        messages.success(request,'Your message has been replied successfully.')

您可以按如下方式进行:创建自定义管理模板。将标准模板从 django 安装复制到目录 yourproject/templates/admin/youapp/yourmodel/change_list.html 将名称 yourproject 等替换为您的名称。

class YourAdmin(admin.ModelAdmin):

    change_list_template = 'admin/yourapp/yourmodel/change_list.html'

然后在表单元素中修改此模板(修改显示在注释行之间 - 只插入了两行):

      {% block pagination %}{% pagination cl %}{% endblock %}

        <!--Inserted code starts here-->

        <input type="message" name="mail_subject">
        <textarea type="message" name="mail_message"><textarea>

        <!--End of inserted code-->

      </form>

    </div>
  </div>

{% endblock %}

然后您可以在操作方法中访问这两个输入的值。

def send_mail(self, request, queryset):
    form_data = request.POST.dict()
    subject= form_data["mail_subject"]
    message = form_data["mail_message"]
    ... and then send your emails