Django-Admin: integrating custom forms

我们两清 提交于 2019-12-09 06:54:50

问题


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.

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?

Traceback

Line 60 is highlighted.

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)

回答1:


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'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.

Extend admin/base_site.html instead and you're good to go.




回答2:


you need to pass to the view's context two more variables:

context['opts'] = RfigTag._meta
context['app_label'] = RfigTag._meta.app_label



回答3:


Maybe you just need do:

class RfidTagAdmin(admin.ModelAdmin):
      form = RfidImport
      ...

Refer to: https://docs.djangoproject.com/en/1.5/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form



来源:https://stackoverflow.com/questions/7889139/django-admin-integrating-custom-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!