我正在尝试使用Django表单向导设置一个多步骤表单,但遇到“NoReverseMatch”错误。
根据以前对此错误的经验,这是因为当网址占用一个时,我没有通过参数,即(?P<pk>\d+)
。
在我的具体案例中: 使用参数'()'和关键字参数'{'step':u'address'}'找不到'task_step'。尝试了0种模式:[]
到目前为止我尝试过:
<form action="">
<form action="">
这与我的urls.py有关吗?
// views.py
FORMS = [("subcategory", ChooseSubcategoryForm),
("address", SetAddressForm),
("task-details", AddTaskDetailsForm),]
TEMPLATES = {"subcategory": "tasks/create_task_form/step1.html",
"address": "tasks/create_task_form/step2.html",
"task-details": "tasks/create_task_form/step3.html",
}
class AddTaskWizard(NamedUrlSessionWizardView):
form_list = [ChooseSubcategoryForm, SetAddressForm, AddTaskDetailsForm]
def get_template_names(self):
return [TEMPLATES[self.steps.current]]
def get_context_data(self, form, **kwargs):
context = super(AddTaskWizard, self).get_context_data(form=form, **kwargs)
# add extra variables for a specific step
# if self.steps.current == "step_name":
# context.update({'another_variable': True})
return context
def done(self, form_list, form_dict, **kwargs):
# do something with the form data(form_list)
subcategory = form_dict['subcategory'].save()
print("============")
print(subcategory)
print("============")
address = form_dict['address'].save()
task = form_dict['task-details'].save()
return HttpResponseRedirect('/tasks')
def get_step_url(self, step):
return reverse(self.url_name, kwargs={'step':step})
// urls.py
from django.conf.urls import url
from .views import CreateTaskView, TaskDetailView
from . import views
from .forms import ChooseSubcategoryForm, SetAddressForm, AddTaskDetailsForm
app_name='tasks'
named_task_forms = (
("subcategory", ChooseSubcategoryForm),
("address", SetAddressForm),
("task-details", AddTaskDetailsForm),
)
task_wizard = views.AddTaskWizard.as_view(named_task_forms, url_name="task_step")
urlpatterns = [
url(r'^add_task/(?P<step>\w+)/$', task_wizard, name='task_step'),
]
// step1.html =&gt;子类别表格
<div class="row" id="create-task-progress">
<div class="container">
<div class="col-md-4 section-static border-top-gray text-center">
<span class="glyphicon glyphicon-search glyphicon-md orange" ></span> <span class="orange">1. Choose Subcategory</span>
</div>
<div class="col-md-4 section-static col-middle text-center">
<span class="glyphicon glyphicon-home glyphicon-md" ></span> <span>2. Set Address</span>
</div>
<div class="col-md-4 section-static border-top-gray text-center">
<span class="glyphicon glyphicon-pencil glyphicon-md"></span> <span>3. Task Details</span>
</div>
</div>
</div>
<div class="row section-light-gray border-top-gray">
<div class="container" id="task-form-container">
<div class="col-md-12">
<form action="{% url 'tasks:task_step' wizard.steps.current %}" method="POST" enctype="multipart/form-data" class="col-md-10 col-md-push-1">
{% csrf_token %}
{{ wizard.management_form }}
<div class="col-md-12 task-field-container">
{{ wizard.form.subcategory.label }}
{{ wizard.form.subcategory }}
</div>
<input type="submit" value="Continue" id="add_task" class="btn btn-primary col-xs-12" />
</form>
</div>
</div>
</div>
答案 0 :(得分:2)
根据我在模板中看到的内容,您使用的是url命名空间。 我相信你需要改变
task_wizard = views.AddTaskWizard.as_view(named_task_forms, url_name="task_step")
到
task_wizard = views.AddTaskWizard.as_view(named_task_forms, url_name="tasks:task_step")
所以你只是错过了tasks: