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

Chapter 9.1 / example 9.1 - return request #154

Closed
briceparent opened this issue Nov 10, 2016 · 1 comment
Closed

Chapter 9.1 / example 9.1 - return request #154

briceparent opened this issue Nov 10, 2016 · 1 comment

Comments

@briceparent
Copy link

In this chapter, you use return request and then explain this use by :

You’ll note that we return back a HttpRequest object rather than an arbitrary value or even a None
object. We do this because as Python is a dynamically typed language, we can attach additional
attributes to the HttpRequest

If you don't return it, and call it with check_sprinkles(request) instead of request = check_sprinkles(request), you'll have the same effect, as this object is mutable (correct me if I'm wrong), so there should be no reason to make a new assignment and this would lead to thinking that we are able to call the function without modifying the object just by removing the assignment.

Is there a reason to make the assignment like you did ?

@pydanny
Copy link
Member

pydanny commented Feb 17, 2017

Yes, that's explained in the example that follows:

from django.core.exceptions import PermissionDenied

def check_sprinkles(request):
    if request.user.can_sprinkle or request.user.is_staff:
        # By adding this value here it means our display templates
        #   can be more generic. We don't need to have
        #   {% if request.user.can_sprinkle or request.user.is_staff %}
        #   instead just using
        #   {% if request.can_sprinkle %}
        request.can_sprinkle = True
        return request

    # Return a HTTP 403 back to the user
    raise PermissionDenied

@pydanny pydanny closed this as completed Feb 17, 2017
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