Django返回 /' q'

发布于 2025-01-27 21:06:14 字数 2102 浏览 3 评论 0原文

django返回 MultivalueDickKeyError在 / 当我尝试将搜索功能添加到我的应用中时,仪表板模板中的'q'。当用户在搜索输入上输入某些内容以返回用户搜索的值时,我想要。但是当我自己尝试做时,我会遇到错误。

MultiValueDictKeyError at /
'q'

def dashboard(request):
    photos = Photo.objects.all()
    query = request.GET['q']
    card_list = Photo.objects.filter(category__contains=query)
    context = {'photos': photos, 'card_list':card_list}
    return render(request, 'dashboard.html', context) 



   <div class="container">
   <div class="row justify-content-center">
     <form action="" method="GET">
        <input type="text" name="q" class="form-control">
      <br>
      <button class="btn btn-outline-success" type="submit">Search</button>
     </form>
  </div>
 </div>   
 <br>
 <div class="container">
      <div class="row justify-content-center">
           {% for photo in photos reversed %}
                    <div class="col-md-4">  
                        <div class="card my-2">
                          <img class="image-thumbail" src="{{photo.image.url}}" alt="Card 
                           image cap">
                       
                          <div class="card-body">
                               <h2 style="color: yellowgreen; font-family: Arial, Helvetica, 
                                sans-serif;">
                               {{photo.user.username.upper}}
                               </h2>
                          <br>
                          <h3>{{photo.category}}</h3>
                          <h4>{{photo.price}}</h4>  
                         </div>
                         <a href="{% url 'Photo-view' photo.id %}" class="btn btn-warning btn- 
                         sm m-1">Buy Now</a>
                       </div>
                 </div>
                 {% empty %}
                 <h3>No Files...</h3>
                 {% endfor %} 
                </div> 
         </div>

django returns MultiValueDictKeyError at /
'q'
in my dashboard template when I'm trying to add search functionality into my app. I want when a user type something on the search input to return the value that user searched for. but i endup getting an error when i try to do it myself.

MultiValueDictKeyError at /
'q'

def dashboard(request):
    photos = Photo.objects.all()
    query = request.GET['q']
    card_list = Photo.objects.filter(category__contains=query)
    context = {'photos': photos, 'card_list':card_list}
    return render(request, 'dashboard.html', context) 



   <div class="container">
   <div class="row justify-content-center">
     <form action="" method="GET">
        <input type="text" name="q" class="form-control">
      <br>
      <button class="btn btn-outline-success" type="submit">Search</button>
     </form>
  </div>
 </div>   
 <br>
 <div class="container">
      <div class="row justify-content-center">
           {% for photo in photos reversed %}
                    <div class="col-md-4">  
                        <div class="card my-2">
                          <img class="image-thumbail" src="{{photo.image.url}}" alt="Card 
                           image cap">
                       
                          <div class="card-body">
                               <h2 style="color: yellowgreen; font-family: Arial, Helvetica, 
                                sans-serif;">
                               {{photo.user.username.upper}}
                               </h2>
                          <br>
                          <h3>{{photo.category}}</h3>
                          <h4>{{photo.price}}</h4>  
                         </div>
                         <a href="{% url 'Photo-view' photo.id %}" class="btn btn-warning btn- 
                         sm m-1">Buy Now</a>
                       </div>
                 </div>
                 {% empty %}
                 <h3>No Files...</h3>
                 {% endfor %} 
                </div> 
         </div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风和你 2025-02-03 21:06:14

尝试一下

query = request.GET['q']
query = request.GET.get('q', '')  # use get to access the q

get()方法用指定的键返回项目的值。

try this

query = request.GET['q']
query = request.GET.get('q', '')  # use get to access the q

The get() method returns the value of the item with the specified key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文