刚开始学习django。 为了维护用户管理,我尝试使用Userena应用程序(如果我没记错的话,Userena是django标准auth app的扩展名。)
我将Userena配置并部署到基于django的网站上没有任何问题。然而,面部照片让我有点头疼:)
我可以注册新用户并选择照片作为照片。图片上传到默认目录“mugshots”(在我的项目的根目录中创建),之后django找不到它 - 错误404.
我的意思是如果我点击指向图片的网址
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.
我猜,根本原因是一些不正确的网址映射,但我不知道如何解决这个问题。请帮忙! :)
根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."
EDITED: 有点解决了...... 只是想分享我的发现。
1,使用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 docs中错过了那个? 我想知道,这对每个人来说都是如此明显吗? :)