0

I have two main usage and main model pages, in which products from a specific usage or model are listed. I have the following views for these pages:

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)

Here we have a get_common_queryset() function, which you can read about the reason of it in this question. Then we have two simillar view functions, products_usage_main and product_model_main but with different templates.

In the urls.py I have following paths for these views:

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

In which, again, we can see that the two paths are similar with just different views.

And finally I have two separate templates for these two views, which their code is not needed or related to the problem I'm facing.

THE PROBLEM:

In my products page sidebar, I have two main links referencing /products/application/ and /products/model/, and when I click on the /products/application/, everything works just fine; but when /products/model/ is clicked, I get the following error:

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

And when I looked into the traceback error, It said that the problem raised from product_detail view and in line product = Product.objects.get(id=pk), which has NOTHING to do with these two pages and their views! Below is my product_detail view:

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)

Below I'm sending complete error traceback in case you need it:

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

1 Answer 1

0

Inorder to solve this problem, I reviewed my other views, specially the product_detail view, and I tried to understand why this view should be the source of this problem.

In my products/urls.py I had a path to product_detail view, which was as below:

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

On the other hand model page url, as mentioned above, was as below:

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

So I guess that the simillarities between these two paths was the source of the problem, and when I changed path url of product_detal to path("product-detail/<str:pk>/, both pages worked just fine.

UPDATE: I found another solution and the main reason for this problem. After changing the product_detail path as mentioned above, it got me thinking about the reason for this problem, because, other than products/model/ I had another url with this structure, products/application/; so, why this problem only occured on model page? Then I checked my urls.py file throughly, and this was full paths in 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",
    ),
]

As you can see the order of the urls, first url is products/application/ and the second one is products/<str:pk>; I think Django checks the urls in this order, and it expects the model in products/model/ to be the <str:pk> part in the second url. So, I changed the order of urls, and it just worked fine without any need to change the product_detail page url.

Final urls.py code:

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",
    ),
]

I hope this answer would be helpfull for others who might encounter a problem like this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.