一切都在我的代碼完美的,但是當我添加(?P<no_of_lines>\d+)
,礦山網址之一後,我已經receveid錯誤:Django的NoReverseMatch在/
NoReverseMatch at /
Reverse for 'day_data_multiadd' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['day_data_multiadd/(?P\d+)/$']
我不知道怎麼回事。我是初學者所以大概這將是很容易的......
views.py:
def get_number_of_lines(request):
if request.method == 'POST':
generate_form = multiadd_generate_form(request.POST)
if generate_form.is_valid():
no_of_lines = generate_form.cleaned_data['no_of_lines']
return HttpResponseRedirect(reverse('multi_add', kwargs={'no_of_lines': no_of_lines}))
else:
generate_form = multiadd_generate_form()
c = RequestContext(request, {'generate_form': generate_form})
return render_to_response('no_lines.html', c)
def day_data_multiadd(request, no_of_lines):
no_of_lines = int(no_of_lines)
CostFormSet = modelformset_factory(Cost, form=data_add_form extra=no_of_lines)
if request.method == 'POST':
formset = CostFormSet(request.POST, request.FILES)
if formset.is_valid():
formset.save()
else:
formset = CostFormSet()
c = RequestContext(request, {'formset': formset})
return render_to_response('multi_add.html', c)
forms.py
class data_add_form(forms.ModelForm):
class Meta:
model = Cost
fields = ['title', 'value', 'publish', 'category']
class multiadd_generate_form(forms.Form):
formy = forms.IntegerField(max_value=30, min_value=1)
class BaseLineFormSet(BaseFormSet):
def __init__(self, *args, **kwargs):
super(BaseLineFormSet, self).__init__(*args, **kwargs)
no_of_forms = len(self)
for i in range(0, no_of_forms):
self[i].fields['formy'].label += "-%d" % (i + 1)
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.current_detail, name='current_detail'),
url(r'^stats$', views.costs_stats, name='costs_stats'),
url(r'^(?P<year>\d{4})/(?P<month>\d{2})/$', views.month_stats_detail, name='month_stats_detail'),
url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/$', views.day_stats_detail, name='day_stats_detail'),
url(r'^(?P<year>\d{4})/$', views.year_stats_detail, name='year_stats_detail'),
url(r'^day_data_multiadd/(?P<no_of_lines>\d+)/$', views.day_data_multiadd, name='day_data_multiadd'),
url(r'^no_line$', views.get_number_of_lines, name='get_number_of_lines'),
url(r'^delete/(?P<id>\d+)/$', views.day_data_delete, name='day_data_delete'),
url(r'^add$', views.data_add, name='data_add'),
]
你共享不會導致異常的代碼。例外提到了名爲「day_data_multiadd」的URL調用,該調用沒有任何(kw)參數。這不會發生在您與我們分享的{views,forms} .py文件中。 – Bjorn
我懷疑在使用{%url'...'%}構建網址的模板中可能有錯誤,因爲r'^ day_data_multiadd /(?P \ d +)/ $'是正確的模式。 –
somecallitblues