Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try/except in examples #9

Closed
sergerdn opened this issue Nov 3, 2020 · 1 comment
Closed

try/except in examples #9

sergerdn opened this issue Nov 3, 2020 · 1 comment

Comments

@sergerdn
Copy link

sergerdn commented Nov 3, 2020

Hi, @pymike00. Thank you very much for your courses on Udemy. It is really nice and productivity.
I suggest for you using another way to using try/except in your courses. I think start coding in the properly way is better for any students.

This one:

def product_detail(request, pk):
    try:
        product = Product.objects.get(pk=pk)
        data = {"product": {
                    "name": product.name,
                    "manufacturer": product.manufacturer.name,
                    "description": product.description,
                    "photo": product.photo.url,
                    "price": product.price,
                    "shipping_cost": product.shipping_cost,
                    "quantity": product.quantity,                   
                }}
        response = JsonResponse(data)
    except Product.DoesNotExist:
        response = JsonResponse({
            "error": {
                "code": 404,
                "message": "product not found!"
            }},
            status=404)
    return response

Should be written as:

def product_detail(request, pk):
    try:
        product = Product.objects.get(pk=pk)
    except Product.DoesNotExist: # we should handle any exceptions as soon as possible
        response = JsonResponse({
            "error": {
                "code": 404,
                "message": "product not found!"
            }},
            status=404)
    else:
        data = {"product": {
                    "name": product.name,
                    "manufacturer": product.manufacturer.name,
                    "description": product.description,
                    "photo": product.photo.url,
                    "price": product.price,
                    "shipping_cost": product.shipping_cost,
                    "quantity": product.quantity,                   
                }}
        response = JsonResponse(data)
    return response
@pymike00
Copy link
Owner

pymike00 commented Nov 4, 2020

Hi @sergerdn.

I agree.

Sometimes little things like this still manage to slip through while recording a video and trying to keep things as easy and straightforward for newcomers as possible.

Thanks for your contribution, it's appreciated.

I am glad you are enjoying the course :)

Best,
Michele

@pymike00 pymike00 closed this as completed Nov 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants