Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 815
bjoern Avatar asked Sep 14 '25 03:09

bjoern


1 Answers

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.

like image 111
Yuji 'Tomita' Tomita Avatar answered Sep 16 '25 23:09

Yuji 'Tomita' Tomita