首页 > 解决方案 > Django 查询不支持的格式字符 '"' (0x22)

问题描述

我将一个 store_id 字段从我的索引页面上的链接传递到视图,以便查找它并提供一个显示商店页面。我正在使用 sqlite 后端,但 Model.objcts.get() 函数不会返回对象。它抛出指向我的 .get 行的错误:

ValueError at /store/1001946/
unsupported format character '"' (0x22) at index 192
Request Method: GET

我的观点.py

def store(request, store_id):
   print(type(store_id))
   sto = StoreStat.objects.get(storeid=store_id)
   return render(request, 'stores/store.html', {'store': sto})

我尝试将类型更改为整数,使用编码,但似乎没有任何效果。我也尝试了 .filter() 因为它相似但我得到了同样的错误

我的应用网址

from django.urls import path, re_path

from . import views

urlpatterns = [
   path('', views.index, name='index'),
   path('store/<store_id>/',views.store, name='store')
]

项目网址

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
  path('', include('stores.urls')),
  path('admin/', admin.site.urls),
]

索引.html

<ol>
      {% for store in top %}
          <li><a href="{% url 'store' store_id=store.storeid%}">{{ 
               store.storestorename }}</a></li>
      {% endfor %}
 </ol>

标签: pythondjangostringsqlite

解决方案


我解决了错误。我的数据库中的一个字段包含一个 % 符号。我不得不回去重命名没有它的列,它起作用了。我想这把整个事情都搞砸了!


推荐阅读