django admin custom template page not found error

2019-09-02 06:16发布

问题:

I am trying to modify the django admin interface and I cannot see the custom templates.

In the admin.py file I added this model admin

class FTPAdmin(admin.ModelAdmin):

      def get_urls(self):
          urls = super(FTPAdmin,self).get_urls()
          my_urls = patterns('',
                    (r'^ftp/$',self.admin_site.admin_view(self.FTPView)))
          return my_urls + urls


      def FTPView(self, request):
          form = FTPForm()
          if request.method == 'POST':
             if form.is_valid():
                return HttpResponseRedirect('/admin/')
          return render_to_response(request,'ftpform.html',{'form':form})

In my project folder I have a template folder with an admin subfolder with the custom template (/DjangoProjects/projectftp/templates/admin/ftp.html)

{% extends "admin/base_site.html" %}
{% block title %}My test upload{% endblock %}

{% block content %}
    <form action="" method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <input type="submit" value="Upload" />
    </form>
{% endblock %}

In my settings.py file I change the template dir:

TEMPLATE_DIRS = (
    BASE_DIR + '/templates/',
)

In my forms.py I create a simple form:

class ProcFTPForm(forms.Form):
      anio = forms.IntegerField(required=True)
      mes = forms.IntegerField(required=True)
      def __init__(self, *args, **kwargs):
          super(ProcFTPForm, self).__init__(*args, **kwargs)
          self.helper = FormHelper()

I try to see the template in the url "ip:port/admin/ftp" or ip:port/admin/app_name/ftp or ip:port/ftp.

Any suggestion

Thanks in advance

UPDATE

this is the code of my model:

class Lectura_FTP():
      def leer_ftp(self):
          cursor = connection.cursor()
          sp = cursor.callproc("CARGA_LISTA_ARCHIVOS_PC")
          cursor.close()
          return sp

回答1:

You should use the render shortcut instead of the render_to_response:

return render(request, 'ftpform.html', {'form': form})

Or change the render_for_response call to this:

return render_for_responce('ftpform.html', {'form': form},
                           context_instance=RequestContext(request))

UPDATE: And url of the view is ip:port/admin/app_name/model_name/ftp/