免责声明:Djano相对较新,因此可以原谅问题中的任何不足之处。
我有一个带有DAL字段的简单表单,我正在尝试提交表单值id和子句以呈现详细信息页面。 DAL提供了预期的结果。提交时,出现以下错误:
django.urls.exceptions.NoReverseMatch: Reverse for 'asset_detail' with arguments '(39526, '')' not found. 1 pattern(s) tried: ['(?P<id>[0-9]+)\\/(?P<slug>[-a-zA-Z0-9_]+)\\/$']
请注意,现有数据的ID在39499处停止。该ID(和39500-39525)是在提交时生成的。我不想将新条目添加到数据库中,而是查找现有条目。网址,表单,视图,模型和模板,请参见下文:
# models.py
class CI(models.Model):
ci_type = models.ForeignKey(Category,
related_name='assets',
on_delete=models.CASCADE,
blank=True,
null=True)
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200,db_index=True)
status = models.BooleanField(default=True)
serial = models.CharField(max_length=140, blank=True)
asset_tag = models.CharField(max_length=53, blank=True)
model_source = models.CharField(max_length=54, blank=True)
vendor = models.CharField(max_length=58, blank=True)
support_level = models.CharField(max_length=60, blank=True)
class Meta:
ordering = ('name',)
index_together = (('id', 'slug'),)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('lookup:asset_detail',
args=[self.id, self.slug])
# forms.py
class AssetForm(forms.ModelForm):
category = forms.ModelChoiceField(
queryset=Category.objects.all(),)
name = forms.ModelChoiceField(
queryset=CI.objects.all(),
widget=autocomplete.ModelSelect2(
url='asset-autocomplete',
forward=(forward.Field('category', 'ci_type'),)))
class Meta:
model = CI
fields = ('name',)
# views.py
class AssetView(CreateView):
model = CI
form_class = AssetForm
template_name = 'lookup/asset.html'
# urls.py
path('<int:id>/<slug:slug>/', views.ci_detail,
name='asset_detail'),
#asset.html (template)
<form action="{{ ci.get_absolute_url }}" method="post">
<label for="asset_type">Select Asset:</label>
{% csrf_token %}
{{ form.as_p }}
<div class="col-4">
<p><input class="btn btn-primary" type="submit" value="Select"></p>
谢谢!