我已经创建了一个用于验证xml文件的类,我尝试了它的工作,但我如何使脚本在我在submit_line.html中创建的新按钮中工作
这是我的admin.py with actions
class CrsFileAdmin(admin.ModelAdmin):
list_display = ('fi', 'file', 'import_datetime', 'state')
readonly_fields = ('state','import_datetime','log')
actions = ['some_action','Validating']
def Validating(self, request, queryset):
for crsfile in queryset:
xsdfile = etree.parse("/home/.../.../.../ex.xsd")
xsdpar = etree.XMLSchema(xsdfile)
xmlfile = etree.parse(crsfile.file.path)
result = xsdpar.validate(xmlfile)
if result == True:
crsfile.log = "Validate: Successfully validate"
crsfile.state=CrsFile.CHECKED
print("valid")
else:
crsfile.state=CrsFile.ERROR
crsfile.log = xsdpar.error_log
print(crsfile.log)
crsfile.import_datetime=timezone.now()
crsfile.save()
self.message_user(request, crsfile.log)
return crsfile.log
Validating.short_description = "XML Validate"
admin_site.register(CrsFile, CrsFileAdmin)
我在submit_line.html
中创建了这个新按钮{% load i18n admin_urls %}
<div class="submit-row">
{% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" name="_save" />{% endif %}
{% if show_delete_link %}
{% url opts|admin_urlname:'delete' original.pk|admin_urlquote as delete_url %}
<p class="deletelink-box"><a href="{% add_preserved_filters delete_url %}" class="deletelink">{% trans "Delete" %}</a></p>
{% endif %}
{% if show_save_as_new %}<input type="submit" value="{% trans 'Save as new' %}" name="_saveasnew" />{% endif %}
{% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother" />{% endif %}
{% if show_save_and_continue %}<input type="submit" value="{% trans 'Save and continue editing' %}" name="_continue" />{% endif %}
#NEW SUBMIT BUTTON
{% if show_save_and_continue %}<input type="submit" value="{% trans 'IMPORT' %}" name="_continue" />{% endif %}
</div>