擅长:python、mysql、java
<p><strong>过滤器</strong>不会引发异常。可能您正在尝试获取一个独特的对象,可以使用<strong>get</strong>这样做:</p>
<pre><code> Manufacturer.objects.get(title__iexact=manufacturer_title)
</code></pre>
<p>现在,如果对象不存在,<strong>get</strong>将引发异常</p>
<p>您在案例中所需的功能也可以通过快捷方式完成。这样地:
如果需要单个对象:</p>
<pre><code> from django.shortcuts import get_object_or_404
def manufacturer(request, manufacturer_title):
m = get_object_or_404(Manufacturer, title__iexact=manufacturer_title)
return HttpResponse("Manufacturer: %s" % manufacturer_title)
</code></pre>
<p>如果需要查询集:</p>
<pre><code> from django.shortcuts import get_list_or_404
def manufacturer(request, manufacturer_title):
m = get_list_or_404(Manufacturer, title__contains=manufacturer_title)
return HttpResponse("Manufacturer: %s" % manufacturer_title)
</code></pre>