' WSGIRequest'对象没有属性' current_app'

时间:2015-09-23 15:37:03

标签: python django django-suit

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

admin.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中的项目并应用操作&#34; action_add_session&#34;我收到以下错误

 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相关,或者我是否看不到明显的错误。

提前感谢您的帮助。

修改

/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()

我看到 suit 调用current_app,我该如何解决?对不起,我在Django几乎是一个新手

1 个答案:

答案 0 :(得分:0)

你应该set request.current_app自己。