Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

Django - add link with custom admin page href

In my Django project, I have created a custom admin page for an app via the get_urls() method. I'd like to add a link to the app's main model index view that will take users to this custom page - however, I'm having some trouble creating this link element correctly and I don't seem to be able to piece together the right way to do it - I'm just left with a Reverse for 'export' not found. 'export' is not a valid view function or pattern name. error.

I've set up the admin for the app like so:

# my_project/observations/admin.py

from django.template.response import TemplateResponse
from django.urls import path

class ObservationAdmin(SimpleHistoryAdmin, SoftDeletionModelAdmin):
    change_list_template = 'export_link.html'

    def get_urls(self):
        urls = super().get_urls()
        custom_urls = [
            path('export/', self.admin_site.admin_view(self.export_view), name='export')
        ]

        return custom_urls + urls

    def export_view(self, request):
        context = dict(
           self.admin_site.each_context(request),
        )

        return TemplateResponse(request, 'export.html', context)

and the two templates that are referenced:

# my_project/observations/templates/export.html

{% extends "admin/base_site.html" %}
{% block content %}
<div>
    Some custom content
</div>
{% endblock %}
# my_project/observations/templates/export_link.html

{% extends 'admin/change_list.html' %}

{% block object-tools-items %}
    <li>
        <a href="{% url 'export' %}" class="btn btn-high btn-success">Export</a>
    </li>
    {{ block.super }}
{% endblock %}

Navigating directly to http://localhost:8000/admin/observations/observation/export/ works perfectly, I see the custom content page exactly as I want it... so the issue I'm striking is with the link template - I get the Reverse... error when I navigate to the model index page.

Perhaps the argument I'm passing to url is incorrect, or I need to register that URL elsewhere - but I don't quite know. The other examples of link elements like this that I've been able to find don't reference URLs created via the admin class' get_urls() method - so any guidance on this would be greatly appreciated.

Thanks very much, let me know if there's any other info that I can provide to help sort this out.

question from:https://stackoverflow.com/questions/66056545/django-add-link-with-custom-admin-page-href

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think the problems is in missing namespace in your export_link.html template. Instead of:

<a href="{% url 'export' %}" class="btn btn-high btn-success">Export</a>

try:

<a href="{% url 'admin:export' %}" class="btn btn-high btn-success">Export</a>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...