Django:视图,模板和url之间的连接出现异常错误

waxmsbnn  于 2022-12-14  发布在  Go
关注(0)|答案(1)|浏览(134)

我有两个main usagemain model页面,其中列出了特定用途或型号的产品。我有这些页面的以下视图:

def get_common_queryset():
    usage_queryset = Usage.objects.all()
    sub_usage_queryset = SubUsage.objects.all()
    main_model_queryset = MainModel.objects.all()
    pump_type_queryset = PumpType.objects.all()
    queryset_dictionary = {
        "usage_queryset": usage_queryset,
        "sub_usage_queryset": sub_usage_queryset,
        "main_model_queryset": main_model_queryset,
        "pump_type_queryset": pump_type_queryset,
    }
    return queryset_dictionary

def products_usage_main(request):
    queryset_dictionary = get_common_queryset()
    context = queryset_dictionary
    return render(request, "products/products_usage_main.html", context)

def products_model_main(request):
    queryset_dictionary = get_common_queryset()
    context = queryset_dictionary
    return render(request, "products/products_model_main.html", context)

这里我们有一个get_common_queryset()函数,你可以在this问题中读到它的原因。然后我们有两个类似的视图函数,products_usage_mainproduct_model_main,但是使用不同的模板。
urls.py中,这些视图的路径如下:

urlpatterns = [
    path("application/", products_usage_main, name="products_usage_main"),
    path("model/", products_model_main, name="products_model_main"),
]

在这里,我们可以再次看到,两条路径是相似的,只是视角不同。
最后,我为这两个视图创建了两个单独的模板,它们的代码与我所面临的问题无关。
问题:
在我的产品页面侧边栏中,有两个引用/products/application//products/model/的主链接,当我单击/products/application/时,一切都正常;但当单击/products/model/时,我得到以下错误:

ValidationError at /products/model/
['“model” is not a valid UUID.']

当我查看回溯错误时,它说问题是从product_detail视图和product = Product.objects.get(id=pk)行中提出的,这与这两个页面和它们的视图无关!下面是我的product_detail视图:

def product_detail(request, pk):
    product = Product.objects.get(id=pk)
    head_flow_dataset = HeadFlowDataSet.objects.filter(
        product=product
    ).order_by("flow")
    context_of_view = {
        "product": product,
        "head_flow_dataset_x": [],
        "head_flow_dataset_y": [],
    }
    for head_flow in head_flow_dataset:
        context_of_view["head_flow_dataset_x"].append(head_flow.flow)
        context_of_view["head_flow_dataset_y"].append(head_flow.head)
    queryset_dictionary = get_common_queryset()
    context = {
        **context_of_view,
        **queryset_dictionary,
    }
    return render(request, "products/product_detail.html", context)

下面我发送完整的错误追溯,如果你需要它:

Internal Server Error: /products/model/
Traceback (most recent call last):
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\db\models\fields\__init__.py", line 2649, in to_python
    return uuid.UUID(**{input_form: value})
  File "C:\Users\Vahid Moradi\AppData\Local\Programs\Python\Python310\lib\uuid.py", line 177, in __init__
    raise ValueError('badly formed hexadecimal UUID string')
ValueError: badly formed hexadecimal UUID string

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\products\views.py", line 152, in product_detail
    product = Product.objects.get(id=pk)
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\db\models\query.py", line 636, in get
    clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs)
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\db\models\query.py", line 1420, in filter
    return self._filter_or_exclude(False, args, kwargs)
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\db\models\query.py", line 1438, in _filter_or_exclude
    clone._filter_or_exclude_inplace(negate, args, kwargs)
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\db\models\query.py", line 1445, in _filter_or_exclude_inplace
    self._query.add_q(Q(*args, **kwargs))
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\db\models\sql\query.py", line 1532, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\db\models\sql\query.py", line 1562, in _add_q
    child_clause, needed_inner = self.build_filter(
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\db\models\sql\query.py", line 1478, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\db\models\sql\query.py", line 1303, in build_lookup
    lookup = lookup_class(lhs, rhs)
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\db\models\lookups.py", line 27, in __init__
    self.rhs = self.get_prep_lookup()
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\db\models\lookups.py", line 341, in get_prep_lookup
    return super().get_prep_lookup()
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\db\models\lookups.py", line 85, in get_prep_lookup
    return self.lhs.output_field.get_prep_value(self.rhs)
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\db\models\fields\__init__.py", line 2633, in get_prep_value
    return self.to_python(value)
  File "D:\Projects\Navid Motor\Website\Django\NavidMotor.com\.venv\lib\site-packages\django\db\models\fields\__init__.py", line 2651, in to_python
    raise exceptions.ValidationError(
django.core.exceptions.ValidationError: ['“model” is not a valid UUID.']
[12/Dec/2022 11:22:53] "GET /products/model/ HTTP/1.1" 500 145337
t40tm48m

t40tm48m1#

为了解决这个问题,我回顾了我的其他视图,特别是product_detail视图,我试图理解为什么这个视图应该是这个问题的根源。
在我的products/urls.py中,我有一个product_detail视图的路径,如下所示:

path("<str:pk>/", product_detail, name="product_detail"),

另一方面,如上所述,model page url如下所示:

path("model/", products_model_main, name="products_model_main"),

所以我猜这两个路径之间的相似性是问题的根源,当我把product_detal的路径url改为path("product-detail/<str:pk>/时,两个页面都工作得很好。
UPDATE:我找到了另一个解决方案,也是这个问题的主要原因。在如上所述更改了product_detail路径之后,它让我思考了这个问题的原因,因为除了products/model/之外,我还有另一个具有这种结构的url,products/application/;那么,为什么这个问题只发生在model页面上呢?然后我彻底检查了我的urls.py文件,这是products.urls中的完整路径:

path("application/", products_usage_main, name="products_usage_main"),
    path("<str:pk>/", product_detail, name="product_detail"),
    path(
        "application/single-application/<str:pk>",
        products_single_usage_list,
        name="products_single_usage_list",
    ),
    path("model/", products_model_main, name="products_model_main"),
    path(
        "application/single-application/sub-application/<str:pk>",
        products_sub_usage_list,
        name="products_sub_usage_list",
    ),
    path(
        "model/single-model/<str:pk>",
        products_single_model_list,
        name="products_single_model_list",
    ),
    path(
        "model/single-model/pump-type/<str:pk>",
        products_single_type_list,
        name="products_single_type_list",
    ),
]

正如您所看到的URL的顺序,第一个URL是products/application/,第二个是products/<str:pk>;我认为Django按照这个顺序检查url,并且它希望products/model/中的model是第二个url中的<str:pk>部分。所以,我改变了url的顺序,它就工作得很好,而不需要改变product_detail页面的url。
最终urls.py代码:

urlpatterns = [
    path("application/", products_usage_main, name="products_usage_main"),
    path("model/", products_model_main, name="products_model_main"),
    path("<str:pk>/", product_detail, name="product_detail"),
    path(
        "application/single-application/<str:pk>",
        products_single_usage_list,
        name="products_single_usage_list",
    ),
    path(
        "application/single-application/sub-application/<str:pk>",
        products_sub_usage_list,
        name="products_sub_usage_list",
    ),
    path(
        "model/single-model/<str:pk>",
        products_single_model_list,
        name="products_single_model_list",
    ),
    path(
        "model/single-model/pump-type/<str:pk>",
        products_single_type_list,
        name="products_single_type_list",
    ),
]

我希望这个答案能对其他可能遇到类似问题的人有所帮助。

相关问题