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

Fix for Django 4.0 removed functions #52

Closed
wants to merge 2 commits into from

Conversation

fraimondo
Copy link

This PR makes is compatible with Django 4.0.

@fraimondo fraimondo mentioned this pull request Oct 5, 2021
@morlandi
Copy link
Owner

Thank you @fraimondo I will merge this soon ;)

@fraimondo
Copy link
Author

Got a better and more "correct" solution. I'll push it soon.

@fraimondo
Copy link
Author

Indeed ugettext_lazy has been an alias of gettext_lazy since Django 2.0. And since the readme states that Django 1.x is not supported anymore, no need to do this try stuff.

@morlandi
Copy link
Owner

Indeed ugettext_lazy has been an alias of gettext_lazy since Django 2.0. And since the readme states that Django 1.x is not supported anymore, no need to do this try stuff.

Totally agree 😉

@morlandi
Copy link
Owner

@fraimondo could you kindly share a link on why request.accepts('application/json') is a suitable replacement for the deprecated is_ajax() ?

@fraimondo
Copy link
Author

That was actually a hard one:

django/django#12091
https://code.djangoproject.com/ticket/30997

By digging in the PR and the associated ticket is_ajax relies on the jquery way of doing ajax calls. Instead of that, the idea is to use the kind of expected content by the request. If the request is expecting a json, then we consider it as an Ajax call.

@morlandi
Copy link
Owner

@fraimondo I tried to merge this PR but got the following exception:

  File "/Users/morlandi/Projects/github_public/django-ajax-datatable/ajax_datatable/views.py", line 371, in dispatch
    if request.accepts('application/json'):
AttributeError: 'WSGIRequest' object has no attribute 'accepts'

runnng the example project with Django v 3.0.8 and Python 3.8.11

By inspecting the requests namespaces, accepts() seems to be missing, indeed:

ipdb> dir(request)
['COOKIES', 'FILES', 'GET', 'META', 'POST', 'REQUEST', 'class', 'delattr', 'dict', 'dir', 'doc', 'eq', 'format', 'ge', 'getattribute', 'gt', 'hash', 'init', 'init_subclass', 'iter', 'le', 'lt', 'module', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'sizeof', 'str', 'subclasshook', 'weakref', '_body', '_current_scheme_host', '_encoding', '_files', '_get_full_path', '_get_post', '_get_raw_host', '_get_scheme', '_initialize_handlers', '_load_post_and_files', '_mark_post_parse_error', '_messages', '_post', '_read_started', '_set_content_type_params', '_set_post', '_stream', '_upload_handlers', 'body', 'build_absolute_uri', 'close', 'content_params', 'content_type', 'encoding', 'environ', 'get_full_path', 'get_full_path_info', 'get_host', 'get_port', 'get_raw_uri', 'get_signed_cookie', 'headers', 'is_ajax', 'is_secure', 'method', 'parse_file_upload', 'path', 'path_info', 'read', 'readline', 'readlines', 'resolver_match', 'scheme', 'session', 'upload_handlers', 'user']

Any suggestion ?

@fraimondo
Copy link
Author

Indeed I just figured out it's specific for Django 4.0. I can think of creating is_ajax in utils.py and doing the if there.

@PetrDlouhy
Copy link
Contributor

@fraimondo According to https://stackoverflow.com/questions/63629935/django-3-1-and-is-ajax I fixed this with following function in django-experiments project:

def is_ajax(request):
    return request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'

@fraimondo
Copy link
Author

@fraimondo According to https://stackoverflow.com/questions/63629935/django-3-1-and-is-ajax I fixed this with following function in django-experiments project:

def is_ajax(request):
    return request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'

The main issue with this approach is that it is exactly why they deprecated and later removed it. The X-Requested-With heather is nonstandard and set by JQuery.

@morlandi
Copy link
Owner

then why not using either request.accepts('application/json') or request.is_ajax() depending on the version of Django as detected at run-time ?

@fraimondo
Copy link
Author

fraimondo commented Dec 16, 2021 via email

@morlandi
Copy link
Owner

never mind, take your time 😉

@morlandi morlandi mentioned this pull request Dec 25, 2021
@morlandi
Copy link
Owner

I finally opted for this:

        try:
            is_ajax_request = request.accepts("application/json")
        except AttributeError as e:
            # Django < 4.0
            is_ajax_request = request.is_ajax()

        if is_ajax_request:
            ...

@morlandi morlandi closed this Dec 27, 2021
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

Successfully merging this pull request may close these issues.

None yet

4 participants