使用 CreateView 类时 Django Admin 中的反向错误

Reverse error in Django Admin when using CreateView class

提问人:Ron 提问时间:11/6/2023 最后编辑:Ron 更新时间:11/7/2023 访问量:36

问:

我在 Django Admin 中有 Customer 和 Appointment 模型。我没有使用内置的“添加”链接来创建新的约会(这将呈现一个可能包含数百个条目的客户下拉控件),而是覆盖了管理员的change_form.html模板,以创建一个 href,该 href 仅使用一个选定的客户创建新的约会。这样,我就可以在创建包含该客户作为字段的新约会之前,利用漂亮的过滤器来查找所需的客户。

我正在使用 CreateView 类并尝试在路由到 Django Admin 的 create 方法之前更改上下文,希望创建一个包含所需customer_id的 Appointment 对象。

但这会返回错误:

使用关键字参数 '{'app_label': ''}' not 反转 'app_list' 发现。尝试了 1 种模式: ['admin/(?P<app_label>auth|长颈鹿)/$']

更改上下文后,我真正想做的是重定向到与 url“admin/appointment/add”对应的管理员视图,但 CreateView 需要模板文件名而不是 url。

这种方法是做我所追求的正确方法吗?

change_form.html

<a href="/admin/appointment/add/116" class="addlink">New Appointment</a>

通过 urls.py 链接:

path('appointment/add/<int:customer_id>', views.NewAppointmentFromLink.as_view())

views.py 中的这门课:

    from django.views.generic import CreateView
    from django.http import HttpResponseRedirect
    
    class NewAppointmentFromLink(CreateView):
            model = Appointment
            fields = ['customer', 'location']
            template_name = "admin/change_form.html" 
        
          def get_context_data(self, **kwargs):
            """Insert the single object into the context dict."""
           context = {}
           context['customer_id'] = self.kwargs['customer_id']
           return super().get_context_data(**context)

这是 Django 错误处理返回的“change_form.html”模板。第 19 行是标记错误的位置。呈现此视图的(未知)视图是 Django Admin 的,无论 ChangeView 路由到什么,我都不会覆盖它。

在模板中 /var/www/ptech/venv/lib/python3.8/site-packages/django/contrib/admin/templates/admin/change_form.html, 第 19 行出错

{% block extrastyle %}{{ block.super }}<link rel="stylesheet" href="{% static "admin/css/forms.css" %}">{% endblock %}
10  
11  {% block coltype %}colM{% endblock %}
12  
13  {% block bodyclass %}{{ block.super }} app-{{ opts.app_label }} model-{{ opts.model_name }} change-form{% endblock %}
14  
15  {% if not is_popup %}
16  {% block breadcrumbs %}
17  <div class="breadcrumbs">
18  <a href="{% url 'admin:index' %}">{% translate 'Home' %}</a>
19  &rsaquo; <a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ opts.app_config.verbose_name }}</a>
20  &rsaquo; {% if has_view_permission %}<a href="{% url opts|admin_urlname:'changelist' %}">{{ opts.verbose_name_plural|capfirst }}</a>{% else %}{{ opts.verbose_name_plural|capfirst }}{% endif %}
21  &rsaquo; {% if add %}{% blocktranslate with name=opts.verbose_name %}Add {{ name }}{% endblocktranslate %}{% else %}{{ original|truncatewords:"18" }}{% endif %}
22  </div>
23  {% endblock %}
24  {% endif %}
25  
26  {% block content %}<div id="content-main">
27  {% block object-tools %}
28  {% if change and not is_popup %}
29    <ul class="object-tools">

以下是app_list的定义:

contrib/admin/sites.py

re_path(regex,wrap(self.app_index), name="app_list"),
蟒蛇 django-admin

评论

0赞 John Gordon 11/6/2023
错误出在视图上,所以这就是我们需要看到的。向我们展示 html 模板和视图的视图代码。app_listapp_list
0赞 Ron 11/7/2023
@JohnGordon我对这个问题做了补充。

答: 暂无答案