有人可以解释一下,为什么我的表单会返回此错误ImportError: No module named 3530
。我发现这发生在get_success_url
,但我无法理解ImportError
与此有什么关系。
我有一个静态表单,它返回一些数据并将其显示在模板中,因此这是FormView
import calendar
import datetime
from django.contrib import messages
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.http import HttpResponseForbidden
from django.shortcuts import redirect, get_object_or_404
from django.views.generic.edit import FormView
from .helpers.user_util import get_user_full_name_or_user_name
from models import Rate
from statistics.forms import CommissionsMonthSelectionForm
from statistics.services import ManagerCommissionsProjectsCalculation, CommissionsAccessService
class UserSalesCommissionsView(FormView):
template_name = "statistics/user_sales_commissions.html"
form_class = CommissionsMonthSelectionForm
currency = Rate.EUR
def get(self, request, *args, **kwargs):
user_id = self.kwargs['user_id']
requesting_user = request.user
if CommissionsAccessService.can_view_commissions_page(requesting_user, user_id):
month = int(self.request.GET.get('month', datetime.datetime.now().month))
year = int(self.request.GET.get('year', datetime.datetime.now().year))
form = CommissionsMonthSelectionForm(initial={'year': year, 'month': month})
context_data = self.get_context_data(user_id, month, year, form=form)
return self.render_to_response(context_data)
else:
return HttpResponseForbidden('Access denied')
def post(self, request, *args, **kwargs):
user_id = self.kwargs['user_id']
requesting_user = self.request.user
if CommissionsAccessService.can_view_commissions_page(requesting_user, user_id):
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
return self.form_valid(form, user_id)
else:
return self.form_invalid(form)
else:
return HttpResponseForbidden('Access denied')
def form_valid(self, form, user_id=None):
year = form.cleaned_data['year']
month = form.cleaned_data['month']
return redirect(self.get_success_url(user_id, month, year))
def form_invalid(self, form):
messages.add_message(self.request, messages.ERROR,
"Invalid data. Please check fields.")
return self.render_to_response(
self.get_context_data(form=form)
)
def get_success_url(self,
user_id=None,
year=datetime.datetime.now().year,
month=datetime.datetime.now().month):
return reverse("vinclucms_statistics:manager_commissions_statistics", user_id) + \
"?month=" + str(month) + "&year=" + str(year)
def get_context_data(self,
user_id=None,
year=datetime.datetime.now().year,
month=datetime.datetime.now().month,
**kwargs):
context = super(UserSalesCommissionsView, self).get_context_data(**kwargs)
context['user_name'] = get_user_full_name_or_user_name(get_object_or_404(User, pk=user_id))
context['can_view'] = True
context['currency'] = self.currency
context['month'] = calendar.month_name[month]
context['year'] = year
context['commissions'] = ManagerCommissionsProjectsCalculation.get_project_list(
self.currency, year, month, user_id)
return context
这个完整的Traceback,
形式:
from django import forms
from statistics.choices import StatisticsChoices
class CommissionsMonthSelectionForm(forms.Form):
year = forms.ChoiceField(choices=StatisticsChoices.YEAR_CHOICES)
month = forms.ChoiceField(choices=StatisticsChoices.MONTH_CHOICES)
URL:
urlpatterns = patterns('',
login_required(views.SupplierInvoiceStatisticsView.as_view()),
name='supplier_invoice_statistics'),
url(r'^statistics/commissions/sales/(?P<user_id>[0-9]+)/$',
)