I was just hosting my django project on heroku and it is getting internal server error

Viewed 43
2022-07-11T09:25:52.180154+00:00 app[web.1]: from django.urls import path, include,re_path,url
2022-07-11T09:25:52.180154+00:00 app[web.1]: ImportError: cannot import name 'url' from 'django.urls' (/app/.heroku/python/lib/python3.10/site-packages/django/urls/__init__.py)
2022-07-11T09:25:52.180154+00:00 app[web.1]: 
2022-07-11T09:25:52.180154+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2022-07-11T09:25:52.180154+00:00 app[web.1]: 
2022-07-11T09:25:52.180155+00:00 app[web.1]: Traceback (most recent call last):
2022-07-11T09:25:52.180155+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
2022-07-11T09:25:52.180155+00:00 app[web.1]: response = get_response(request)
2022-07-11T09:25:52.180158+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/whitenoise/middleware.py", line 60, in __call__
2022-07-11T09:25:52.180158+00:00 app[web.1]: response = self.get_response(request)
2022-07-11T09:25:52.180158+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/core/handlers/exception.py", line 57, in inner
2022-07-11T09:25:52.180158+00:00 app[web.1]: response = response_for_exception(request, exc)
2022-07-11T09:25:52.180159+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/core/handlers/exception.py", line 139, in response_for_exception
2022-07-11T09:25:52.180159+00:00 app[web.1]: response = handle_uncaught_exception(
2022-07-11T09:25:52.180159+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/core/handlers/exception.py", line 183, in handle_uncaught_exception
2022-07-11T09:25:52.180159+00:00 app[web.1]: callback = resolver.resolve_error_handler(500)
2022-07-11T09:25:52.180160+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/urls/resolvers.py", line 710, in resolve_error_handler
2022-07-11T09:25:52.180160+00:00 app[web.1]: callback = getattr(self.urlconf_module, "handler%s" % view_type, None)
2022-07-11T09:25:52.180160+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/utils/functional.py", line 49, in __get__
2022-07-11T09:25:52.180160+00:00 app[web.1]: res = instance.__dict__[self.name] = self.func(instance)
2022-07-11T09:25:52.180160+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/urls/resolvers.py", line 689, in urlconf_module
2022-07-11T09:25:52.180160+00:00 app[web.1]: return import_module(self.urlconf_name)
2022-07-11T09:25:52.180161+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/importlib/__init__.py", line 126, in import_module  
2022-07-11T09:25:52.180161+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2022-07-11T09:25:52.180162+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
2022-07-11T09:25:52.180162+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
2022-07-11T09:25:52.180162+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
2022-07-11T09:25:52.180162+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
2022-07-11T09:25:52.180162+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 883, in exec_module
2022-07-11T09:25:52.180162+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
2022-07-11T09:25:52.180163+00:00 app[web.1]: File "/app/myweb1/urls.py", line 17, in <module>
2022-07-11T09:25:52.180163+00:00 app[web.1]: from django.urls import path, include,re_path,url
2022-07-11T09:25:52.180163+00:00 app[web.1]: ImportError: cannot import name 'url' from 'django.urls' (/app/.heroku/python/lib/python3.10/site-packages/django/urls/__init__.py)
2022-07-11T09:25:52.180164+00:00 app[web.1]:
2022-07-11T09:25:52.180164+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2022-07-11T09:25:52.180164+00:00 app[web.1]:
2022-07-11T09:25:52.180164+00:00 app[web.1]: Traceback (most recent call last):
2022-07-11T09:25:52.180164+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
2022-07-11T09:25:52.180165+00:00 app[web.1]: response = get_response(request)
2022-07-11T09:25:52.180165+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/utils/deprecation.py", line 134, in __call__
2022-07-11T09:25:52.180165+00:00 app[web.1]: response = response or self.get_response(request)
2022-07-11T09:25:52.180165+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/core/handlers/exception.py", line 57, in inner
2022-07-11T09:25:52.180166+00:00 app[web.1]: response = response_for_exception(request, exc)
2022-07-11T09:25:52.180166+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/core/handlers/exception.py", line 139, in response_for_exception
2022-07-11T09:25:52.180166+00:00 app[web.1]: response = handle_uncaught_exception(
2022-07-11T09:25:52.180167+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/core/handlers/exception.py", line 183, in handle_uncaught_exception
2022-07-11T09:25:52.180167+00:00 app[web.1]: callback = resolver.resolve_error_handler(500)
2022-07-11T09:25:52.180167+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/urls/resolvers.py", line 710, in resolve_error_handler
2022-07-11T09:25:52.180168+00:00 app[web.1]: callback = getattr(self.urlconf_module, "handler%s" % view_type, None)
2022-07-11T09:25:52.180168+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/utils/functional.py", line 49, in __get__
2022-07-11T09:25:52.180168+00:00 app[web.1]: res = instance.__dict__[self.name] = self.func(instance)
2022-07-11T09:25:52.180169+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/urls/resolvers.py", line 689, in urlconf_module
2022-07-11T09:25:52.180169+00:00 app[web.1]: return import_module(self.urlconf_name)
2022-07-11T09:25:52.180169+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/importlib/__init__.py", line 126, in import_module  
2022-07-11T09:25:52.180170+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2022-07-11T09:25:52.180170+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
2022-07-11T09:25:52.180170+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
2022-07-11T09:25:52.180170+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
2022-07-11T09:25:52.180170+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
2022-07-11T09:25:52.180171+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 883, in exec_module
2022-07-11T09:25:52.180171+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
2022-07-11T09:25:52.180174+00:00 app[web.1]: File "/app/myweb1/urls.py", line 17, in <module>
2022-07-11T09:25:52.180174+00:00 app[web.1]: from django.urls import path, include,re_path,url
2022-07-11T09:25:52.180174+00:00 app[web.1]: ImportError: cannot import name 'url' from 'django.urls' (/app/.heroku/python/lib/python3.10/site-packages/django/urls/__init__.py)
2022-07-11T09:25:52.180174+00:00 app[web.1]:
2022-07-11T09:25:52.180174+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2022-07-11T09:25:52.180175+00:00 app[web.1]:
2022-07-11T09:25:52.180175+00:00 app[web.1]: Traceback (most recent call last):
2022-07-11T09:25:52.180175+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/workers/sync.py", line 136, in handle
2022-07-11T09:25:52.180175+00:00 app[web.1]: self.handle_request(listener, req, client, addr)
2022-07-11T09:25:52.180175+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/workers/sync.py", line 179, in handle_request
2022-07-11T09:25:52.180175+00:00 app[web.1]: respiter = self.wsgi(environ, resp.start_response)
2022-07-11T09:25:52.180176+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/core/handlers/wsgi.py", line 132, in __call__
2022-07-11T09:25:52.180176+00:00 app[web.1]: response = self.get_response(request)
2022-07-11T09:25:52.180176+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/core/handlers/base.py", line 140, in get_response
2022-07-11T09:25:52.180176+00:00 app[web.1]: response = self._middleware_chain(request)
2022-07-11T09:25:52.180176+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/core/handlers/exception.py", line 57, in inner
2022-07-11T09:25:52.180176+00:00 app[web.1]: response = response_for_exception(request, exc)
2022-07-11T09:25:52.180177+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/core/handlers/exception.py", line 139, in response_for_exception
2022-07-11T09:25:52.180177+00:00 app[web.1]: response = handle_uncaught_exception(
2022-07-11T09:25:52.180177+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/core/handlers/exception.py", line 183, in handle_uncaught_exception
2022-07-11T09:25:52.180177+00:00 app[web.1]: callback = resolver.resolve_error_handler(500)
2022-07-11T09:25:52.180178+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/urls/resolvers.py", line 710, in resolve_error_handler
2022-07-11T09:25:52.180178+00:00 app[web.1]: callback = getattr(self.urlconf_module, "handler%s" % view_type, None)
2022-07-11T09:25:52.180178+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/utils/functional.py", line 49, in __get__
2022-07-11T09:25:52.180178+00:00 app[web.1]: res = instance.__dict__[self.name] = self.func(instance)
2022-07-11T09:25:52.180178+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/django/urls/resolvers.py", line 689, in urlconf_module
2022-07-11T09:25:52.180178+00:00 app[web.1]: return import_module(self.urlconf_name)
2022-07-11T09:25:52.180179+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/importlib/__init__.py", line 126, in import_module  
2022-07-11T09:25:52.180179+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2022-07-11T09:25:52.180179+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
2022-07-11T09:25:52.180179+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
2022-07-11T09:25:52.180179+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
2022-07-11T09:25:52.180179+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
2022-07-11T09:25:52.180179+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 883, in exec_module
2022-07-11T09:25:52.180179+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
2022-07-11T09:25:52.180180+00:00 app[web.1]: File "/app/myweb1/urls.py", line 17, in <module>
2022-07-11T09:25:52.180180+00:00 app[web.1]: from django.urls import path, include,re_path,url
2022-07-11T09:25:52.180180+00:00 app[web.1]: ImportError: cannot import name 'url' from 'django.urls' (/app/.heroku/python/lib/python3.10/site-packages/django/urls/__init__.py)
2022-07-11T09:25:52.180301+00:00 app[web.1]: 10.1.35.183 - - [11/Jul/2022:09:25:52 +0000] "GET / HTTP/1.1" 500 0 "-" "-"

here i got the error. My urls.py file:

from django.contrib import admin
from django.urls import path, include,re_path,url
from django.conf import settings
from django.conf.urls.static import static
from django.views.static import serve

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('shopping.urls')),
     re_path(r'^media/(?P<path>.*)$', serve,{'document_root':       settings.MEDIA_ROOT}), 
    re_path(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}), 
]

and settings.py

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
STATIC_URL= "/static/"
STATIC_ROOT = os.path.join(BASE_DIR,'staticfiles')
MEDIA_ROOT= os.path.join(BASE_DIR, "media")
MEDIA_URL="/media/"
1 Answers

The url(…) function [Django-doc] has been removed since in favor of the re_path(…) function [Django-doc] You can find this in the release notes [Django-doc].

You thus should remove url from the import, so:

from django.urls import path, include, re_path  # no url

I would strongly advise you to use the same Django version in development and production, since likely a lot of other features will (eventually become) deprecated, and thus this makes it hard to debug on a production server.


Note: You should not use Django's functionality to serve media files or static files in production. The serve(…) function [Django-doc] is inefficient and likely unsafe. The Django documentation explains how to serve media and static files through Nginx, Apache and other mechanisms that are optimized to serve static content. Especially since these can work with caches, and will omit a lot of logic that Django uses.

Related