Skip to content

Commit

Permalink
Add support for Django 4 (#24)
Browse files Browse the repository at this point in the history
* Use path instead of url to support Django 4

The old way of defining urls in DJango is no longer supported in Django 4.0.

This commit replaces the regular expression based paths by the more modern path method, and also adds app_name in the urls file to allow usage with URL namespaces.

* Increase version

Altered major version because old versions of Django cannot be supported anymore.
  • Loading branch information
jeduardo211 authored May 26, 2022
1 parent af0fa38 commit d819f97
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 26 deletions.
2 changes: 1 addition & 1 deletion error_tracker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# :license: BSD-3-Clause
#

__version__ = '2.1.0'
__version__ = '3.0.0'
__author__ = 'Sonu Kumar'
__email__ = '[email protected]'

Expand Down
2 changes: 1 addition & 1 deletion error_tracker/django/templates/error_tracker/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<div class="container">
{% block header_block %}
<h2 class="text-center mb-5 mt-3">
<a href="{% url 'view_errors' %}" class="text-dark home-link">{% trans 'Errors Seen' %}</a>
<a href="{% url 'error_tracker:view_errors' %}" class="text-dark home-link">{% trans 'Errors Seen' %}</a>
</h2>
{% endblock %}
{% block content_block %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
<td>{{ error.host }}</td>
<td width="2%">{{ error.method }}</td>
<td>
<a class="view-link" href="{% url 'view_error' rhash=error.hash %}">
<a class="view-link" href="{% url 'error_tracker:view_error' rhash=error.hash %}">
{{ error.path|truncatechars:30 }}
</a>
</td>
<td>{{ error.exception_name }}</td>
<td>{{ error.last_seen }}</td>
<td>{{ error.count }}</td>
<td>
<a href="{% url 'delete_error' rhash=error.hash %}"
<a href="{% url 'error_tracker:delete_error' rhash=error.hash %}"
class="delete btn btn-danger btn-sm">{%trans 'Delete' %}</a>
</td>
</tr>
Expand Down
9 changes: 5 additions & 4 deletions error_tracker/django/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
# :license: BSD-3-Clause
#

from django.conf.urls import url
from django.urls import path
from .views import detail, view_list, delete_exception

app_name = 'error_tracker'
urlpatterns = [
url(r'^$', view_list, name="view_errors"),
url(r'^(?P<rhash>[\w-]+)/delete$', delete_exception, name='delete_error'),
url(r'^(?P<rhash>[\w-]+)$', detail, name='view_error'),
path('', view_list, name="view_errors"),
path('<rhash>/delete', delete_exception, name='delete_error'),
path('<rhash>', detail, name='view_error'),
]
9 changes: 5 additions & 4 deletions error_tracker/django/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ def view_list(request):
error = False
errors = model.get_exceptions_per_page(**query)

next_url = reverse('view_errors') + "?page=" + str(errors.next_num) \
next_url = reverse('error_tracker:view_errors') + "?page=" + str(errors.next_num) \
if errors.has_next else None

prev_url = reverse('view_errors') + "?page=" + str(errors.prev_num) \
prev_url = reverse('error_tracker:view_errors') + "?page=" + str(errors.prev_num) \
if errors.has_prev else None

if request.is_ajax() or request.GET.get('ajax_partial'):
is_ajax = request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
if is_ajax or request.GET.get('ajax_partial'):
table = render_to_string('error_tracker/partials/partial_table.html', {
'errors': errors,
})
Expand All @@ -75,7 +76,7 @@ def delete_exception(request, rhash):
:return: redirect back to home page
"""
model.delete_entity(rhash)
return redirect(reverse('view_errors'))
return redirect(reverse('error_tracker:view_errors'))


@require_GET
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def grep(attrname):
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Topic :: Software Development :: Libraries :: Python Modules',
]
)
16 changes: 8 additions & 8 deletions tests/DjangoTest/DjangoTest/drf_urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.conf.urls import url, include
from django.urls import path, include
from django.contrib import admin
from rest_framework import routers
from error_tracker.django import urls
Expand All @@ -9,11 +9,11 @@
router.register(r'users', UserViewSet)

urlpatterns = [
url('admin/', admin.site.urls),
url("dev/", include(urls)),
url(r'^$', views.index),
url(r'^value-error$', views.value_error),
url(r'^post-view$', views.post_view),
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls'))
path('admin/', admin.site.urls),
path('dev/', include(urls)),
path('', views.index),
path('value-error', views.value_error),
path('post-view', views.post_view),
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls'))
]
12 changes: 6 additions & 6 deletions tests/DjangoTest/DjangoTest/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from django.urls import path, include
from django.contrib import admin
from error_tracker.django import urls
from core import views

urlpatterns = [
url('admin/', admin.site.urls),
url("dev/", include(urls)),
url(r'^$', views.index),
url(r'^value-error$', views.value_error),
url(r'^post-view$', views.post_view),
path('admin/', admin.site.urls),
path('dev/', include(urls)),
path('', views.index),
path('value-error', views.value_error),
path('post-view', views.post_view),
]

0 comments on commit d819f97

Please sign in to comment.