我有一个非常简单的django网站,但我无法获取我在管理面板中显示的图像.
我settings.py
有这些常数:
# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = '/home/jeroen/programming/python/django/sitename/media'
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = 'http://127.0.0.1:8000/media/'
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/media/'
Run Code Online (Sandbox Code Playgroud)
我urls.py
看起来像这样:
from django.conf.urls.defaults import *
from django.conf import settings
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^sitename/', include('sitename.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
# http://docs.djangoproject.com/en/dev/howto/static-files/
# This method is inefficient and insecure.
# Do not use this in a production setting.
# Use this only for development.
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)
Run Code Online (Sandbox Code Playgroud)
我models.py
看起来像这样:
from django.db import models
class Truitje(models.Model):
titel = models.CharField(max_length=50)
beschrijving = models.TextField(max_length=500)
foto = models.ImageField(upload_to='truitjes')
def __unicode__(self):
return self.titel
Run Code Online (Sandbox Code Playgroud)
我可以在管理界面中成功上传图片并将其存储起来/home/jeroen/programming/python/django/sitename/media/truitjes
.但是当我举个例子时,http://127.0.0.1:8000/media/truitjes/DSC00068.JPG
我得到一个错误:Page not found: /media/truitjes/DSC00068.JPG
.同样的http://127.0.0.1:8000/media/truitjes
,和`` http://127.0.0.1:8000/media/gives
权限被拒绝:/ media /`.
Fel*_*ing 11
将您的媒体或管理URL /前缀更改为其他内容.
它们不能具有相同的值.如果他们这样做,ADMIN_MEDIA_PREFIX
则优先.这意味着如果您尝试访问http://127.0.0.1:8000/media
,则尝试访问管理媒体文件夹.
要么改变ADMIN_MEDIA_PREFIX
:
ADMIN_MEDIA_PREFIX = '/admin-media/'
Run Code Online (Sandbox Code Playgroud)
或改变MEDIA_ROOT
:
# in settings.py
MEDIA_ROOT = '/home/jeroen/programming/python/django/ninikske/static'
MEDIA_URL = 'http://127.0.0.1:8000/static/'
# and in url.conf
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
Run Code Online (Sandbox Code Playgroud)
在Django 1.3中,如果您只是想在开发过程中尝试在视图中显示图像,请按照以下步骤操作:
在你的app/urls.py中:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
#..rest of url.py config...
urlpatterns += staticfiles_urlpatterns()
Run Code Online (Sandbox Code Playgroud)在您的模板中,无论您希望图片显示在哪里,都可以使用此模板标记:
<img src="{{ STATIC_URL }}pic.png" />
Run Code Online (Sandbox Code Playgroud)看看开发中的静态文件服务:https://docs.djangoproject.com/en/1.3/howto/static-files/#staticfiles-in-templates
归档时间: |
|
查看次数: |
11187 次 |
最近记录: |