如何自定义Django管理员

时间:2019-04-14 05:34:01

标签: django admin

此代码正常工作。但是我希望以某种形式编写主题和消息以便答复,而不是在答复功能中定义主题消息。如何在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.')

1 个答案:

答案 0 :(得分:0)

您可以按照以下步骤进行操作:创建自定义管理模板。将Django安装中的标准模板复制到yourproject / templates / admin / youapp / yourmodel / change_list.html目录中 将名称yourproject等替换为您的名称。

class YourAdmin(admin.ModelAdmin):

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

然后您在form元素中修改此模板(修改显示在注释行之间-仅插入两行):

      {% 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