1

我在我的管理员中在 PythonAnywhere 中托管的 Django Web 项目中使用 Django-Filer。我已经阅读了安装说明,但无法访问 Filer 为每个文件创建的规范 url。似乎我被定向到 Filer.urls 无法识别的扩展 url(非规范部分从 /filer-public/ 开始;这是一个正在创建的目录,并将我的文件存储在单独的 PythonAnywhere 文件目录中) .

我的网址语法有错误吗?我的views.canonical有错误?我不确定为什么插入确切的规范 url 会将我重定向到该 url 的扩展版本。

Python:3.7 Django:2.2

规范网址:/filer/sharing/1560887480/39/

错误/调试屏幕

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.

应用网址:/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'
    ),
]

应用程序视图:/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)

基本网址:/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')),
]

基本设置:/home/mywebsite/mywebsite/settings.py

FILER_CANONICAL_URL = 'sharing/'
4

2 回答 2

2

通过在我的 url 和设置文件中配置静态和媒体根,我能够使规范 url 工作,如下所示。

网址.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)

设置.py

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

MEDIA_URL = '/home/mywebsite/media/'
于 2019-06-24T20:23:34.383 回答
0

我能够通过不包括filer.urls而是直接在我的项目urls.py文件中指定 url 模式来解决这个问题。

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/'

但是,我仍然不确定最初的额外空间是从哪里来的——据我所知,文件管理器 urls.py 和 settings.py 看起来不错。

于 2021-11-26T00:17:52.873 回答