Django-Admin:整合自定义表单 [英] Django-Admin: integrating custom forms

查看:251
本文介绍了Django-Admin:整合自定义表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为给定模型的管理界面建立一个CSV文件的导入表单。可以从模型的change_list访问视图,但会引发模板语法错误。

I am trying to build a import form for a CSV file into the admin interface for a given model. the view can be reached from the change_list of the model, but it throws a template syntax error.

我做错了什么?我必须创建自己的模板,或者我可以重新使用现有的admin / change_form.html,但是我不明白吗?

What am I doing wrong? Do I have to create my own template or can I re-use the existing admin/change_form.html somehow and I just don't get it?

追溯

第60行突出显示。

Template error:
In template site-packages\django\contrib\admin\templates\admin\change_form.html,
    error at line 60
Caught KeyError while rendering: 'opts'
50 : {% endfor %}
51 : 
52 : {% block after_field_sets %}{% endblock %}
53 : 
54 : {% for inline_admin_formset in inline_admin_formsets %}
55 :     {% include inline_admin_formset.opts.template %}
56 : {% endfor %}
57 : 
58 : {% block after_related_objects %}{% endblock %}
59 : 
60 :  {% submit_row %}
61 : 
62 : {% if adminform and add %}
63 :    <script type="text/javascript">document.getElementById("{{ adminform.first_field.id_for_label }}").focus();</script>
64 : {% endif %}
65 : 
66 : {# JavaScript for prepopulated fields #}
67 : {% prepopulated_fields_js %}
68 : 
69 : </div>
70 : </form></div>

views.py

def import_tags(request):
    if request.method == "POST":
        form = RfidImport(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            success = True
            context = {"form": form, "success": success}
            return HttpResponseRedirect("../")
    else:
        form = RfidImport()
        context = {"form": form}
        return render_to_response("admin/change_form.html", context,
            RequestContext(request))

forms.py

class RfidImport(forms.ModelForm):
    file_to_import = forms.FileField()

    class Meta:
        model = RfidTag
        fields = ("file_to_import", )

    def save(self, commit=False, *args, **kwargs):
        form_input = RfidImport()
        file_csv = self.cleaned_data['file_to_import']
        csv.register_dialect('excel-new', delimiter=';', quoting=csv.QUOTE_NONE)
        records = csv.reader(file_csv, dialect='excel-new')
        for line in records:
            self.system = line[0]
            self.tagId = line[1]
            self.serial = line[2]
        form_input.save()
    datafile.close()

admin.py

class RfidTagAdmin(admin.ModelAdmin):
    list_display = ('tagId','serial', 'system', 'user')

    def get_urls(self):
        urls = super(RfidTagAdmin, self).get_urls()
        my_urls = patterns('',
            (r'^import/$', self.admin_site.admin_view(import_tags))
        )
        return my_urls + urls

    pass

admin.site.register(RfidTag, RfidTagAdmin)


推荐答案

您绝对需要使用自己的模板,或修改更改表单,但也可以修改更改视图。例如,将此导入添加到更改表单本身中应该是微不足道的。

You definitely need to use your own template, or modify the change form but also modify the change view. For example, it should be trivial to add this import into the change form itself.

Django的管理员为其管理员使用了很多神奇的东西,这些模板有很多标签这些特定于通过其更改/更改列表视图传递的对象。

Django's admin uses a lot of magical things for its admin, and those templates have many tags that are specific to the objects passed in via its change/changelist views.

代替延长 admin / base_site.html ,你很好。

这篇关于Django-Admin:整合自定义表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆