2016-11-24 212 views
0

一切都在我的代碼完美的,但是當我添加(?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'), 
] 
+2

你共享不會導致異常的代碼。例外提到了名爲「day_data_multiadd」的URL調用,該調用沒有任何(kw)參數。這不會發生在您與我們分享的{views,forms} .py文件中。 – Bjorn

+0

我懷疑在使用{%url'...'%}構建網址的模板中可能有錯誤,因爲r'^ day_data_multiadd /(?P \ d +)/ $'是正確的模式。 – somecallitblues

回答

0

我已經找到了答案:

views.py

def day_data_multiadd(request, no_of_lines=0): 
no_of_lines = int(no_of_lines) 
CostFormSet = modelformset_factory(Cost, form=DataAddForm, extra=no_of_lines) 

if request.method == 'POST' and 'form' in request.POST: 
    formset = CostFormSet(request.POST, request.FILES) 
    if formset.is_valid(): 
     formset.save() 
else: 
    formset = CostFormSet(queryset=Cost.objects.none()) 

if request.method == 'POST' and 'no_line' in request.POST: 
    generate_form = MultiaddGenerateForm(request.POST) 
    if generate_form.is_valid(): 
     cd = generate_form.cleaned_data 
     return HttpResponseRedirect(reverse('core_sm:day_data_multiadd', args=(cd['formy'],))) 
else: 
    generate_form = MultiaddGenerateForm() 

return render(request, 'core_sm/costs/multi_add.html', {'formset': formset, 
                 'no_of_lines': no_of_lines, 
                 'generate_form': generate_form}) 

url.py

url(r'^day_data_multiadd/$', views.day_data_multiadd, name='day_data_multiadd'), 
url(r'^day_data_multiadd/(?P<no_of_lines>\d+)/$', views.day_data_multiadd, name='day_data_multiadd'), 
+0

我的答案中缺少的是什麼? –

1

你叫這個地方:

{% url day_data_multiadd %} 

,你應該提供的行數:如果你想添加一個默認值

{% url day_data_multiadd number_of_lines %} 

,所以第一個例子會工作,您必須添加其他網址格式並在視圖中提供默認值:

... 
url(r'^day_data_multiadd/$', views.day_data_multiadd, name='day_data_multiadd'), 
url(r'^day_data_multiadd/(?P<no_of_lines>\d+)/$', views.day_data_multiadd, name='day_data_multiadd'), 
... 

... 
def day_data_multiadd(request, no_of_lines=0): 
    ...