我正在尝试使用基本表单将数据添加到我的应用程序中,但我已经设置了一个结构,将所有内容分成“项目”。
所有内容都会被您选择的项目过滤掉,并且项目会为用户提供更多密钥。
然后我的网址设置为:
** //后端/项目
** //后端/项目[PROJID] /能力
然后,当我添加'capability'元素时,我需要从URL或kwargs中读取'project'id
如何访问capability_project(在呈现表单模板时)
即。提交按钮将用户重定向回正确的项目页面
<form id="capability-form" method="post" action="backend/projects/{{ project_capability }} /capabilities">
我认为因为它有很多对它返回迭代器错误。我该怎么做?如何在添加项目时确保project_id值始终正确?
//编辑以更新NoReverseMatch
错误即时无法访问表单中的kwargs。
NoReverseMatch at /backend/projects/1/capabilities/add/
Reverse for 'capability-list' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method: POST
Request URL: http://c9589d:8000/backend/projects/1/capabilities/add/
并进一步跟踪信息
Request information
GET No GET data
POST Variable Value
status u'12'
domain u'2'
capability_num u'1'
level u'a'
description u'asasd'
submit u'Create Capability'
project u'1'
csrfmiddlewaretoken u'RW2vMwu3BhCOOhDTRaSeCglDSBCctqeF'
name u'asdsd'
models.py
class Project(models.Model):
proj_name = models.CharField()
assigned_to = models.ManyToManyField(User, related_name='assigned_to')
class Capability(models.Model):
name = models.CharField()
project = models.ManyToManyField(Project)
current_status = models.BooleanField()
future_status = models.NullBooleanField()
class Function(models.Model):
name = models.CharField()
capability = models.ForeignKey(Capability)
current_status = models.BooleanField()
future_status = models.NullBooleanField()
views.py
class Add_Capability(CreateView):
template_name = 'backend/add_capability.html'
model = Capability
fields = ['name', 'description', 'status', 'capability_num', 'project', 'domain']
form_class = CapabilityForm
forms.py
class CapabilityForm(forms.ModelForm):
name = forms.CharField(max_length=255, help_text="Please enter a name for the capability")
description = forms.CharField(max_length=255, help_text="Please enter a description for the capability")
status = models.CharField(max_length=255, choices=STATUS_CHOICES, default="Approved")
capability_num = forms.DecimalField(max_digits=5, decimal_places=4, help_text="Enter a number")
project = models.ManyToManyField(Project, null=True)
level = models.CharField(max_length=1, choices=LEVELS_CHOICES, default="Approved")
domain = models.ManyToManyField(Domain)
class Meta:
model = Capability
fields = ('name', 'description',)
urls.py
# /backend/1/capabilities
url(r'^projects/(?P<capability_project>\d+)/capabilities/$', views.CapabilityList.as_view(), name='capability-list'),
# /backend/capabilities/add - Add capabilities
url(r'^projects/(?P<capability_project>\d+)/capabilities/add/$', views.Add_Capability.as_view(), name='add-capability'),
# /backend/projects
url(r'^projects/$', views.ProjectList.as_view(), name='projects'),
答案 0 :(得分:-1)
在views.py中,您可以通过以下方式访问kwargs:
class Add_Capability(CreateView):
template_name = 'backend/add_capability.html'
model = Capability
fields = ['name', 'description', 'status', 'capability_num', 'project', 'domain']
form_class = CapabilityForm
project_id = False
def get_success_url(self):
self.success_url = reverse('project-detail')+self.kwargs['capability_project']+'/'
return super(Add_Capability, self).get_success_url()