Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

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

Django admin actions bar missing? Template expecting "action_form" context variable but mine is showing "action_confirm_form"

As the title says, I'm having an issue that I cant seem to find any reference of anywhere else online. I was working on creating a custom Django admin action that provides and intermediate page for additional request data before updating the selected models. I followed these posts to accomplish this: Displaying an Admin Action Popup and Displaying an Admin Action Confirmation Page. I got everything working and then I went back to add some comments and after the server restarted when detecting changes, the admin action bar completely disappeared (see screenshot below). Another thing to note, my Django project has two separate apps, each with their own admin site and the actions drop-down disappeared from both sites for all model's change list view.

Admin Actions Missing

When inspecting the context of the admin site using the debug_toolbar I see this:

 {'action_confirm_form': <ActionForm bound=False, valid=Unknown, fields=(action;select_across)>,
 'actions_on_bottom': False,
 'actions_on_top': True,
 'actions_selection_counter': True,
 'available_apps': ...

And when looking at the admin site context of a different clone of the repo with the admin actions working as expected I see this:

{'action_form': <ActionForm bound=False, valid=Unknown, fields=(action;select_across)>,
 'actions_on_bottom': False,
 'actions_on_top': True,
 'actions_selection_counter': True,
 'available_apps': ...

It appears that the context variable action_form was renamed to action_confirm_form which is why it is not displaying, because if you look at the admin/change_list.html template, you see where this is rendered:

{% block result_list %}
  {% if action_form and actions_on_top and cl.show_admin_actions %}{% admin_actions %}{% endif %}
    {% result_list cl %}
  {% if action_form and actions_on_bottom and cl.show_admin_actions %}{% admin_actions %}{% endif %}
{% endblock %}

I manually printed out the actions_on_top and cl.show_admin_actions variables, which both evaluated to True, but the action_form variable being renamed causes this entire conditional to be False. I tried manually overriding that template and change the first conditional to check for the existence of the action_confirm_form, which produced this output, the "Go" button to perform the action was rendered, but not the admin actions drop-down:

Admin Actions Missing but Go Button Rendered

At this point, I was fairly certain that I did not manually change anything that would affect the naming of that form in the admin's context, but I switched back to my master branch to run a diff command and to my surprise the problem appeared there too, when it had just started happening on my feature branch that has not been merged into master yet. That led me to believe that there was some error in the way that Pycharm was running the server, but running the code from the the terminal produced the same issue. From there, I tried copying the changes I was working on into an old version of the repo I had cloned somewhere else and the admin site was working as expected there (this is where the second template code segment comes from). So then I copied all of that code back into my main repo and the bug appeared again, which kind of killed my hypothesis that Pycharm was somehow responsible, since two identical copies of the same codebase produced a working and broken instance of the app.

Sorry this was a very long ramble-type post, I tried to show all the debugging steps I already took to avoid any suggestions that I may have already tried. After seeing what I explained in the last paragraph, I'm completely lost and hoping somebody have have some insight or a different approach to this problem. Thanks for taking the time to read this and possibly helping out, it's very appreciated. Oh, and sorry for the image links, I believe I don't have enough rep to include inline images.

Edit:

On the model admin class, I added the following, which printed the actions that I have defined as expected, which means they should be passed to the template, but the issue still persists.

class MyModelAdmin(admin.ModelAdmin):
    ...
    actions = [<MY LIST OF ACTION FUNCTIONS>]
    def get_actions(self, request):
        actions = super().get_actions(request)
        logger.debug(f'ACTIONS: {actions}')
        return actions

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

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...