没有产品与给定的查询匹配 - 搜索表单

时间:2014-02-12 07:14:18

标签: django django-forms django-views

我正在尝试在Django中创建一个搜索表单,但我收到了这个错误:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/search/?q=cddsc
No Product matches the given query.

这是我的表格:

<form class="search_form" action="/search/" method="get">
  <input type="text" name="q" Placeholder="Buscar">
  <button type="button" class="btn btn-primary btn-lg" value="Search">
  <span class="glyphicon glyphicon-star"> Buscar</span>
  </button>                 
</form>

这是我的网址:

url(r'^search/$', 'reviews.views.search'),

这是我的观点:

def search(request):
    if 'q' in request.GET and request.GET['q']:
        message = 'You search for: %r' % request.GET['q']
        return render(request,'search.html', {'msg':message})
    else:
        message = 'You submited an empty form'
        return render(request,'home.html', {'msg':message})

该消息返回没有产品匹配,但我还没有查询产品型号。为什么Django会返回此消息?

这是我的产品型号:

class Product(models.Model):
    name = models.CharField(max_length=500)
    #inserir campo imagem
    #inserir slugify na url do produto
    slug = models.SlugField(max_length=500)
    category = models.ForeignKey(Category)
    image = models.ImageField(upload_to='thumbs/')
    created = models.DateTimeField(auto_now=True, auto_now_add=False)
    updated = models.DateTimeField(auto_now=True, auto_now_add=True)

    def reviews(self):
        return Review.objects.filter(product = self.pk)

    def __unicode__(self):
        return u'%s' % self.name

1 个答案:

答案 0 :(得分:1)

更改

return render('search.html', {'msg':message})

return render(request, 'search.html', {'msg':message})

<form class="search_form" action="search" method="get">
<input type="text" name="" Placeholder="Buscar">

<form class="search_form" action="/search/" method="get">
<input type="text" name="q" Placeholder="Buscar">
<input type="submit" value="Search" />

我找到了“没有产品与给定查询匹配”的来源。信息。在urls.py

    url(r'^(?P<slug>[^/]+)/$', 'reviews.views.single_product'),

之前执行
    url(r'^search/$', 'reviews.views.search'),

请更改urls.py

来自

    url('^accounts/logout/$', 'django.contrib.auth.views.logout'),
    #produt
    #url(r'^(?P<slug>.*)/$', 'single_product'),
    url(r'^(?P<slug>[^/]+)/$', 'reviews.views.single_product'),
    #submit reviews
    url('^(?P<slug>[^/]+)/review_submit/$','reviews.views.single_product'),
    #search
    url(r'^search/$', 'reviews.views.search'),    
)

    url('^accounts/logout/$', 'django.contrib.auth.views.logout'),
    #search
    url(r'^search/$', 'reviews.views.search'),
    #produt
    #url(r'^(?P<slug>.*)/$', 'single_product'),
    url(r'^(?P<slug>[^/]+)/$', 'reviews.views.single_product'),
    #submit reviews
    url('^(?P<slug>[^/]+)/review_submit/$','reviews.views.single_product'),
)