title: Django: How can I make a custom admin view that edits a JSON file, rather than a model?
body: <p>My goal is to add an extra view to my admin page that edits a json file, rather than my database. The <a href="https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#adding-views-to-admin-sites" rel="nofollow noreferrer">documentation</a> says that custom views can be added by overriding the <code>get_urls()</code> method in my custom <code>AdminSite</code> class. It goes on to say:</p>
  
  <blockquote>
    <p>Any view you render that uses the admin templates, or extends the base admin template, should set <code>request.current_app</code> before rendering the template. It should be set to either <code>self.name</code> if your view is on an <code>AdminSite</code> or <code>self.admin_site.name</code> if your view is on a <code>ModelAdmin</code>.</p>
  </blockquote>
  
  <p>As you can see here, I tried doing just that (see the first line of the <code>json_admin_view()</code> method):</p>
  
  <pre><code>import json
  
  from django.contrib.admin import AdminSite
  from django.urls import path
  from django.shortcuts import render
  
  from .forms import JsonForm
  
  JSON_FILE = 'json_app/data.json'
  
  class MyAdminSite(AdminSite):
      site_header = "JSON App Administration"
  
      def get_urls(self):
          urls = super().get_urls()
          my_urls = [
              path('json_admin/',
                   self.admin_view(self.json_admin_view),
                   name='json_admin')
          ]
          return my_urls + urls
  
      def json_admin_view(self, request):
          request.current_app = self.name  # This doesn't seem to do anything
  
          context = {}
  
          with open(JSON_FILE, 'r') as read_data:
              json_data = json.load(read_data)
  
          if request.method == 'POST':
              form = JsonForm(request.POST)
              if form.is_valid():
                  with open(JSON_FILE, 'w') as write_data:
                      json.dump(form.cleaned_data, write_data)
                      context.update({'form_saved': True})
  
          else:
              form = JsonForm({
                  'fname': json_data['fname'],
                  'lname': json_data['lname'],
              })
  
          context.update({'form': form})
  
          return render(request, 'json_app/admin/json_admin.html', context)
  </code></pre>
  
  <p>I'm able to visit <code>/admin/json_admin/</code>, and it works fine, except the header shows "Django Administration" instead of "JSON App Administration", even though <code>json_admin.html</code> extends <code>admin/base_site.html</code>. Also, I would like to have a link to the view show up on the admin index page, as if it were a normal modal change form. So I have two questions:</p>
  
  <ol>
  <li>How come I'm not seeing "JSON App Administration" on the header of my custom admin page? I assume <code>request.current_app = self.name</code> is meant to help with that, but perhaps I'm using it incorrectly.</li>
  <li>How do I add a link to the custom admin page from the admin index? If it were a model I could just use <code>register()</code>, but I can't do that since it's not a model.</li>
  </ol>
  
  <p>If you know a better way of editing a json file from the admin page, I'd be happy to hear that as well.</p>