1

刚开始学习django。为了维护用户管理,我尝试使用 Userena 应用程序(如果我没记错的话,Userena 是 django 标准身份验证应用程序的扩展)。

我将 Userena 配置并部署到基于 django 的网站中没有问题。然而,面部照片让我有点头疼:)

我可以注册新用户并选择一张照片作为面部照片。图片被上传到默认目录“mugshots”(在我的项目的根目录中创建),之后 django 找不到它 - 错误 404。

我的意思是如果我点击引用图片的 URL

http://localhost:8000/accounts/laban/mugshots/2e0973e4d4.jpg

我得到答复

Page not found (404)
Request Method: GET
Request URL:    http://localhost:8000/accounts/laban/mugshots/
Using the URLconf defined in pilot.urls, Django tried these URL patterns, in this order:
^$
^accounts/ ^signup/$ [name='userena_signup']
^accounts/ ^signin/$ [name='userena_signin']
^accounts/ ^signout/$ [name='userena_signout']
^accounts/ ^password/reset/$ [name='userena_password_reset']
^accounts/ ^password/reset/done/$ [name='userena_password_reset_done']
^accounts/ ^password/reset/confirm/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$ [name='userena_password_reset_confirm']
^accounts/ ^password/reset/confirm/complete/$
^accounts/ ^(?P<username>[\.\w-]+)/signup/complete/$ [name='userena_signup_complete']
^accounts/ ^activate/(?P<activation_key>\w+)/$ [name='userena_activate']
^accounts/ ^activate/retry/(?P<activation_key>\w+)/$ [name='userena_activate_retry']
^accounts/ ^(?P<username>[\.\w-]+)/email/$ [name='userena_email_change']
^accounts/ ^(?P<username>[\.\w-]+)/email/complete/$ [name='userena_email_change_complete']
^accounts/ ^(?P<username>[\.\w-]+)/confirm-email/complete/$ [name='userena_email_confirm_complete']
^accounts/ ^confirm-email/(?P<confirmation_key>\w+)/$ [name='userena_email_confirm']
^accounts/ ^(?P<username>[\.\w-]+)/disabled/$ [name='userena_disabled']
^accounts/ ^(?P<username>[\.\w-]+)/password/$ [name='userena_password_change']
^accounts/ ^(?P<username>[\.\w-]+)/password/complete/$ [name='userena_password_change_complete']
^accounts/ ^(?P<username>[\.\w-]+)/edit/$ [name='userena_profile_edit']
^accounts/ ^(?P<username>(?!signout|signup|signin)[\.\w-]+)/$ [name='userena_profile_detail']
^accounts/ ^page/(?P<page>[0-9]+)/$ [name='userena_profile_list_paginated']
^accounts/ ^$ [name='userena_profile_list']
^mugshots/(?P<path>.*)$
The current URL, accounts/laban/mugshots/, didn't match any of these.

我想,根本原因是一些不正确的 url 映射,但我不知道如何解决这个问题。请帮忙!:)

根 urls.py 是:

from django.conf.urls import patterns, include, url
from pilot import settings
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
                       url(r'^$', include('zlab.pilot.index.urls')),
                       (r'^accounts/', include('userena.urls')),
    # Examples:
    # url(r'^$', 'www.views.home', name='home'),
    # url(r'^www/', include('www.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
)

# desperate copy/paste...
# Serve media files only in development
if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^mugshots/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
   )
else:
    print "no server is configured to serve media files. Do it now."

编辑:有点解决了......只是想分享我的发现。

第一,使用 MEDIA_ROOT 更新 settings.py。将 MEDIA_ROOT 设置为指向文件系统上的绝对位置。例如:

MEDIA_ROOT = '/path/to/project'

2、更新网址格式。添加

url(r'^accounts/[\.\w-]+/(mugshots/.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
url(r'^accounts/[\.\w-]+/edit/(mugshots/.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
    }),

我想知道,如果我这样做正确吗?我想知道,我是否在 userena 文档中错过了这一点?我想知道,这对每个人来说都那么明显吗?:)

4

0 回答 0