Django UrlConf NoReverseMatch

时间:2013-04-24 06:05:37

标签: django forms url

以下代码仅在NoReverseMatch(第三个urlconf)上返回browse_years。我错过了什么?谢谢你的帮助!

url(r'^browse/things/(?P<thing>[\w-]+)/$', 'views.browse.things', name='browse_things'),
url(r'^browse/colors/(?P<color>[\w-]+)/$', 'views.browse.colors', name='browse_colors'),
url(r'^browse/years/(?P<year>\d+)/$', 'views.browse.years', name='browse_years'),

def browse(request):
    thing_list = Thing.objects.all()
    if request.method == 'POST':
        form1 = ThingBrowseForm(request.POST)
        form2 = ColorBrowseForm(request.POST)
        form3 = YearBrowseForm(request.POST)
        if form1.is_valid():
            thing = form1.cleaned_data['thing']
            return HttpResponseRedirect(reverse('browse_things', kwargs={'thing':thing}))
        elif form2.is_valid():
            color = form2.cleaned_data['color']
            return HttpResponseRedirect(reverse('browse_colors', kwargs={'color':color}))
        elif form3.is_valid():
            year = form3.cleaned_data['year']
            return HttpResponseRedirect(reverse('browse_years', kwargs={'year':year}))
    else:
        form1 = ThingBrowseForm()
        form2 = ColorBrowseForm()
        form3 = YearBrowseForm()
    return render(request, 'browse.html', {'form1':form1, 'form2':form2, 'form3':form3, 'thing_list':thing_list})

编辑:添加了追溯

Traceback:
File "/lib/python2.7/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/myproject/views/browse.py" in browse
  18.       return HttpResponseRedirect(reverse('browse_colors', kwargs={'color':color}))
File "/lib/python2.7/django/core/urlresolvers.py" in reverse
  476.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/lib/python2.7/django/core/urlresolvers.py" in _reverse_with_prefix
  396.                 "arguments '%s' not found." % (lookup_view_s, args, kwargs))

Exception Type: NoReverseMatch at /browse/
Exception Value: Reverse for 'browse_colors' with arguments '()' and keyword arguments '{'color': u''}' not found.

编辑:添加了forms.py和模板

class ThingBrowseForm(forms.Form):
    thing = forms.ModelChoiceField(queryset=Thing.objects.all(), widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))

class ColorBrowseForm(forms.Form):
    color = forms.ChoiceField(choices=COLORS_EMPTY, required=False, widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))

class YearBrowseForm(forms.Form):
    year = forms.ChoiceField(choices=YEARS_EMPTY, widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))

<form action='' method='post' enctype='multipart/form-data'>
{{ form1.as_p }}
{% csrf_token %}
</form>
<form action='' method='post' enctype='multipart/form-data'>
{{ form2.as_p }}
{% csrf_token %}
</form>
<form action='' method='post' enctype='multipart/form-data'>
{{ form3.as_p }}
{% csrf_token %}
</form>

0 个答案:

没有答案