1

I am using Django-Filer in my admin on a Django web project which is hosted in PythonAnywhere. I have gone through the installation instructions but I am having trouble accessing the canonical urls the Filer makes for each file. It seems I am being directed to an extended url that Filer.urls is not recognizing (the non-canonical part starts at /filer-public/; this is a directory that is being created and storing my files on the separate PythonAnywhere file directory).

Is there an error in my urls syntax? An error in my views.canonical? I am unsure of why plugging in the exact canonical url redirects me to this extended version of the url.

Python: 3.7 Django: 2.2

Canonical URL: /filer/sharing/1560887480/39/

ERROR / DEBUGGING SCREEN

Page not found (404)
Request Method:     GET
Request URL:    http://www.mywebsite.com/filer/sharing/1560887480/39/filer_public/36/ea/36ea58a8-f59c-41ad-9d1f-00a976603eb1/big1.jpg

Using the URLconf defined in mywebsitesite.urls, Django tried these URL patterns, in this order:

    admin/
    ^filer/ sharing/(?P<uploaded_at>[0-9]+)/(?P<file_id>[0-9]+)/$ [name='canonical']

The current path, filer/sharing/1560887480/39/filer_public/36/ea/36ea58a8-f59c-41ad-9d1f-00a976603eb1/big1.jpg, didn't match any of these.

APP URLS: /mywebsite/.virtualenvs/env/lib/python3.7/site-packages/filer/urls.py

# -*- coding: utf-8 -*-
from __future__ import absolute_import

from django.conf.urls import url

from . import settings as filer_settings
from . import views


urlpatterns = [
    url(
        filer_settings.FILER_CANONICAL_URL + r'(?P<uploaded_at>[0-9]+)/(?P<file_id>[0-9]+)/$',  # flake8: noqa
        views.canonical,
        name='canonical'
    ),
]

APP VIEWS: /mywebsite/.virtualenvs/env/lib/python3.7/site-packages/filer/VIEWS.py

# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from django.http import Http404
from django.shortcuts import get_object_or_404, redirect

from .models import File


def canonical(request, uploaded_at, file_id):
    """
    Redirect to the current url of a public file
    """
    filer_file = get_object_or_404(File, pk=file_id, is_public=True)
    if (not filer_file.file or int(uploaded_at) != filer_file.canonical_time):
        raise Http404('No %s matches the given query.' % File._meta.object_name)
    return redirect(filer_file.url)

BASE URLS: /home/mywebsite/mywebsite/urls.py

from django.contrib import admin
from django.urls import include, path
from django.conf.urls import url
from django.views.generic import TemplateView
from quotes.views import Register

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('django.contrib.auth.urls')),
    path('', include('pages.urls')),
    url(r'^filer/', include('filer.urls')),
]

BASE SETTINGS: /home/mywebsite/mywebsite/settings.py

FILER_CANONICAL_URL = 'sharing/'
JcoOnline
  • 243
  • 2
  • 13
  • I don't know Django-Filter, but perhaps you could try changing `FILER_CANONICAL_URL` to `'/sharing/'` with a leading slash and see if that helps? – Giles Thomas Jun 24 '19 at 19:37

2 Answers2

2

I was able to get the canonical url to work by configuring the static and media root in my urls and settings files, as shown below.

urls.py

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('django.contrib.auth.urls')),
    url(r'^filer/', include('filer.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

settings.py

MEDIA_ROOT = os.environ.get('FILER_MEDIA_ROOT', os.path.join(BASE_DIR, 'media'))

MEDIA_URL = '/home/mywebsite/media/'
JcoOnline
  • 243
  • 2
  • 13
0

I was able to solve this by not including the filer.urls but rather specifying the url pattern directly in my projects urls.py file.

Django 3.1.7

django-filer 2.1.2

/myproject/myproject/urls.py:

...
from filer import views as filer_views
from .settings import FILER_CANONICAL_URL
...

urlpatterns = [
  url(r'^filer/'+FILER_CANONICAL_URL+r'/(?P<uploaded_at>[0-9]+)/(?P<file_id>[0-9]+)/$',
      filer_views.canonical,
      name='canonical'),
  ...,
]

/myproject/myproject/settings.py

...
FILER_CANONICAL_URL = 'somefolder/'

However I'm still not sure where the extra space was coming from in the first place - the filer urls.py and settings.py look fine as far as I can tell.