1

在我的 AdminSite (Django Suit 0.2.14 + Django 1.7.8) 中,我使用一个操作来激活所选项目的进程。

管理员.py

class DealAdminBase(AdminVideoMixin, admin.ModelAdmin):

  form = DealAdminForm

  actions = [
    'action_add_session',
  ]
  ...

  def get_urls(self):
    from django.conf.urls import patterns, url
    original_urls = super(DealAdminBase, self).get_urls()

    info = self.model._meta.app_label, self.model._meta.model_name

    my_urls = patterns('',
        url(r'^stats/$', self.admin_site.admin_view(self.view_stats), name='stats'),
        url(r'^add_session/(?P<deal_pk>\d+)/$', self.admin_site.admin_view(self.view_add_session), name='%s_%s_add_session' % info),
    )
    return my_urls + original_urls

  def action_add_session(self, request, queryset):
    if len(queryset) > 1:
        self.message_user(request, 'Select only one.')
        return

    info = self.model._meta.app_label, self.model._meta.model_name
    deal_pk = queryset[0].pk

    return HttpResponseRedirect(reverse('admin:%s_%s_add_session' % info, args=[deal_pk,]))

  def view_add_session(self, request, deal_pk):
     deal = get_object_or_404(self.model, pk=deal_pk)
     has_session = bool(deal.active_session)

     def _make_add_session_form(_session):
        fields = {
            'number': forms.IntegerField(min_value=1),
        }
        if not _session:
            fields['first_start'] = forms.DateTimeField(widget=SuitSplitDateTimeWidget)

        return type('AddSessionForm', (forms.BaseForm,), { 'base_fields': fields })

    form = _make_add_session_form(has_session)(request.POST or None)

    if form.is_valid():
        deal.numero_totale += form.cleaned_data['number']
        deal.numero_rimanente += form.cleaned_data['number']
        deal.save()

        if not has_session:
            deal.create_session(form.cleaned_data['first_start'])

        info = self.model._meta.app_label, self.model._meta.model_name
        return HttpResponseRedirect(reverse('admin:%s_%s_changelist' % info))

    return render_to_response(
        'admin/deal/add_session.html', {
            'form': form,
        }, RequestContext(request))

当我在 /admin 中选择项目并应用操作“action_add_session”时,我收到以下错误

 Request Method:    GET
 Request URL:      http://192.168.33.10:8000/admin/deal/dealgroup/add_session/19/
 Django Version:    1.7.8
 Exception Type:    AttributeError
 Exception Value:   
 'WSGIRequest' object has no attribute 'current_app'
 Exception Location:    /vagrant/.virtualenv/local/lib/python2.7/site-ackages/suit/templatetags/suit_menu.py in get_menu, line 33


Error during template rendering

In template /vagrant/.virtualenv/local/lib/python2.7/site- packages/suit/templates/suit/menu.html, error at line 15
'WSGIRequest' object has no attribute 'current_app'
 5  <div class="left-nav" id="left-nav">
 6    <ul>
 7      {% block menu_home %}
 8        {% url 'admin:index' as index_url %}
 9        <li{% if index_url == request.path %} class="active"{% endif %}>
10          <a href="{{ index_url }}"><i class="icon-home"></i>{% trans   'Home' %}
11          </a>
12        </li>
13      {% endblock %}
14  
15      **{% get_menu request as app_list %}**

我想知道这个错误是否与 django suit 相关,或者我没有看到明显的问题。

预先感谢您的任何帮助。

编辑

/lib/python2.7/site-packages/suit/templatetags/suit_menu.py 中get_menu的代码

@register.assignment_tag(takes_context=True)
def get_menu(context, request):
  """
  :type request: WSGIRequest
  """
 if not isinstance(request, WSGIRequest):
    return None

 # Try to get app list
 try:
    template_response = get_admin_site(context.current_app).index(request)
except NoReverseMatch:
    # Django 1.8 uses request.current_app instead of context.current_app
    template_response = get_admin_site(request.current_app).index(request)

try:
    app_list = template_response.context_data['app_list']
except Exception:
    return

return Menu(context, request, app_list).get_app_list()

我看到西装调用 current_app,我该如何解决?抱歉,但我几乎是 Django 的新手

4

1 回答 1

0

你应该自己设置request.current_app

于 2016-10-01T08:27:26.553 回答