简体   繁体   中英

Displaying Django Admin Navigation on non-model template

I have made an HTML form(with only HTML's form tag, not Django) which I have put inside {% block content %} for Django to display.

{% extends "admin/base_site.html" %}
{# mainapp/templates/VideoTracker.html #}
{% block content_title %}
    {{ 'Execution Status' }} 
{% endblock %}
{% block content %}
<form>
<table> <br>
         <tr> 
             <td> 
                <div>
                    <input type="checkbox" name="adhocexec", value="Adhoc" checked/> Adhoc Request
                </div>
             </td> 
             <td> 
                <div>
                    <input type="checkbox" name="periodicexec", value="Periodic" checked> Periodic Request
                </div>
             </td>
        </tr>
        <tr>
            <td>
                <div>
                    Start : <input type="date" name="frdt"> 
                </div>
            </td>
            <td>
                <div>
                    End : <input type="date" name="todt"> 
                </div>
            </td>
        </tr>
</table>
<br>
<div>
    <button type="submit" class="btn btn-primary"> <span class="glyphicon glyphicon-search"></span> Search Reports </button>
</div>
<br>
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search in results..." title="Type Here">

<table id="myTable">
  <tr class="header">
    <th>BOX ID</th>
    <th>MSO ID</th>
    <th>Execution Type</th>
    <th>Channel ID</th>
    <th>Execution Date Time</th>
    <th>Result</th>
    <th>Detailed Report</th>
  </tr>

    {% for queue in results %}
        <tr>
            <td>{{ queue.box_id }}</td>
            <td>{{ queue.mso_id }}</td>
            <td>{{ queue.exec_type }}</td>
            <td>{{ queue.channel_id }}</td>
            <td>{{ queue.exec_time }}</td>
            <td>{{ queue.result_status }}</td>
            <td>{{ queue.result_link }}</td>
        </tr>
    {% endfor %}
</table>

<script>
function myFunction() 
{   
    var input, filter, table, tr, td, i;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    table = document.getElementById("myTable");
    tr = table.getElementsByTagName("tr");
    for (i = 0; i < tr.length; i++) 
    {
        td_col0 = tr[i].getElementsByTagName("td")[0];
        td_col1 = tr[i].getElementsByTagName("td")[1];
        td_col2 = tr[i].getElementsByTagName("td")[2];
        td_col3 = tr[i].getElementsByTagName("td")[3];
        td_col4 = tr[i].getElementsByTagName("td")[4];
        td_col5 = tr[i].getElementsByTagName("td")[5];
        td_col6 = tr[i].getElementsByTagName("td")[6];
        if (td_col0 && td_col1 && td_col2 && td_col3 && td_col4 && td_col5 && td_col6) 
        {
             cond0 = td_col0.innerHTML.toUpperCase().indexOf(filter) > -1
             cond1 = td_col1.innerHTML.toUpperCase().indexOf(filter) > -1
             cond2 = td_col2.innerHTML.toUpperCase().indexOf(filter) > -1
             cond3 = td_col3.innerHTML.toUpperCase().indexOf(filter) > -1
             cond4 = td_col4.innerHTML.toUpperCase().indexOf(filter) > -1
             cond5 = td_col5.innerHTML.toUpperCase().indexOf(filter) > -1
             cond6 = td_col6.innerHTML.toUpperCase().indexOf(filter) > -1
              if (cond0 || cond1 || cond2 || cond3 || cond4 || cond5 || cond6) 
              {
                tr[i].style.display = "";
              }
              else
              {
                tr[i].style.display = "none";
              }
         }  
      }
}
</script>
</form>
{% endblock %}

I have linked this HTML on urls.py and from my views.py I am calling a function which does some processing and displays data on the HTML. So far so good.

Now I wanted to display this HTML on admin as a link, ideally, all Class Model is shown as the link on Admin but for my case, the data which I am presenting is non-model. So to display this as a clickable link on Admin I made test model on models.py

class TestModel(models.Model):
    try:
        print('TestModel')
    except Exception as ex:
        logging.error('Exception : models --> TestModel ' + str(ex))

And in my admin.py I made class with base AdminViews

class TestAdmin(AdminViews):
    admin_views = (
                    ('Track Executions', 'redirect_to_executions'),
                  )

    def get_model_perms(self, request):
        """
        Return empty perms dict thus hiding the model from admin index.
        """
        return {}

    def redirect_to_executions(self, *args, **kwargs):
        return redirect('/track_execution')

With this, I am able to see link on the Admin page

在此处输入图片说明

After clicking the link, I see default admin page like I am not logged in.

在此处输入图片说明

How can I make my logged in details ie same context to persist as with model links to be shown on my non-model template as well?

在此处输入图片说明

I am a bit struggling with it.

You can use do it at the lazy way or the hard way...

The lazy way Build onde Proxy Models, so that way he wont persist the data, and you can show it as any other models in admin, and change what you want to do with it at your admin view

Proxy Models: https://docs.djangoproject.com/en/2.0/topics/db/models/#proxy-models

The Hard way You can override this template and add it manualy with the same classes and css stuffs to looks like the others.

Override Template: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-admin-templates

Maybe there is other simple way to do that, if you find one better please share :)

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