You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django中get_redirect_url()引发404错误,收藏店铺功能受阻

Troubleshooting "No Store matches the given query" Error in Store Liking Feature

Hey there, let's break down why you're hitting that "No Store matches the given query" error when building your store bookmarking feature. The issue is almost certainly linked to how self.kwargs.get("domainKey") is working (or not working) in your view. Let's walk through the most likely causes and fixes:

1. Your URL route isn't capturing the domainKey parameter

First, double-check your URL configuration. If your route doesn't explicitly define a domainKey parameter to capture from the URL path, self.kwargs won't have that value at all.

For example, your route should look something like this (using Django as an example, since the error pattern matches Django's DoesNotExist exception):

# urls.py
from django.urls import path
from .views import LikeStoreView

urlpatterns = [
    # Make sure <str:domainKey> is included in the path
    path('my-domain/like/<str:domainKey>/', LikeStoreView.as_view(), name='like-store'),
]

If the route is missing <str:domainKey>, self.kwargs.get("domainKey") will return None, and trying to query a Store with None will throw the error you're seeing.

2. The domainKey from the URL doesn't exist in your database

Even if you're capturing the parameter, the value in your URL (like fake-domain in your example) might not match any domainKey in your Store table.

Add a quick print statement in your view to verify what value you're actually getting:

# Inside your view method
domain_key = self.kwargs.get("domainKey")
print(f"Received domainKey: {domain_key}")  # Check your console for this output

Then cross-reference that value with your Store records—if there's no match, that's exactly why the query fails. You might need to fix the URL you're testing with, or ensure the Store entry has the correct domainKey value.

3. Your query isn't handling missing/incorrect values gracefully

If you're using Store.objects.get(domainKey=domain_key), Django will throw a DoesNotExist error (which translates to the message you're seeing) if no matching record is found. Instead, use get_object_or_404 to handle this case more cleanly, or add a check for the parameter first:

from django.shortcuts import get_object_or_404
from django.http import HttpResponseBadRequest
from .models import Store

def your_view_method(self, request, *args, **kwargs):
    domain_key = self.kwargs.get("domainKey")
    
    # First, check if the parameter exists
    if not domain_key:
        # Return a bad request response instead of crashing
        return HttpResponseBadRequest("Missing store domain key")
    
    # Use get_object_or_404 to return a 404 if no store exists
    store = get_object_or_404(Store, domainKey=domain_key)
    
    # Proceed with your liking logic here (e.g., toggle user's favorite status)
    if request.user in store.liked_users.all():
        store.liked_users.remove(request.user)
    else:
        store.liked_users.add(request.user)
    store.save()
    ...

Quick Debug Checklist

  • Confirm your URL route includes the <str:domainKey> parameter
  • Print the domainKey value in your view to ensure it's being captured correctly
  • Verify the captured value matches a domainKey in your Store database table
  • Replace direct Store.objects.get() calls with get_object_or_404 for better error handling

内容的提问来源于stack exchange,提问作者Jay P.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:56:23