我有一个任务,可以通过发送发布请求来更改背景颜色
我的Proj_Url:
path('exercises /',include('exercises.urls')),
我的App_Url:
path('set_color/', SetColorView.as_view(), name='colors'),
这是我的表格:
COLORS = (
('black', 'black'),
('white', 'white'),
('red', 'red'),
('yellow', 'yellow'),
('blue', 'blue'), )
class SetColorForm(forms.Form):
background_color = forms.ChoiceField(choices=COLORS, widget=forms.widgets.RadioSelect)
我的模板
{% extends 'base.html' %}
{% block content %}
<form action="" method="post" style="background-color:{{
form.field.value|default:'powderblue'}}">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Set"/>
</form>
{% endblock %}
我的观点
class SetColorView(View):
form_class = SetColorForm
template_name = 'exercises/colors.html'
def get(self, request):
form = self.form_class
context = {
'form': form,
}
return render(request, self.template_name, context)
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
success_url = 'exercises:colors'
if form.is_valid():
back_color = form.cleaned_data['background_color']
return redirect(reverse(success_url, kwargs={'form': back_color}))
我得到的例外:找不到带有关键字参数'{'form':'red'}'的'colors'。尝试了1种模式:['锻炼\ / set_color \ / $']
我的反向操作错了什么?