简体   繁体   中英

Django-Admin: integrating custom forms

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)

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM