私の 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 でアイテムを選択してアクション「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 スーツに関連している可能性があるのか 、それとも明白に表示されていないのか疑問に思っています。
よろしくお願いします。
編集
/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 の初心者です