有问题。1)在生产中,Django 尝试在 http://"ip_address"/images/"image".jpg URL 访问照片,但是图像必须在 http://"ip_address"/media/images/"image".jpg 2)我在现有模型中添加了另外两个字段(alt 和 text)。迁移工作并显示已添加字段,但我无法在管理面板中看到它们(我已将它们添加到 list_display 中)并且我无法在我的模板中访问它们。
settings for media
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = 'http://"ip_address"/media/'
urls
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
url(r'^$','main.views.mainpage'),
)+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
models
from django.db import models
class Slider(models.Model):
image = models.ImageField(upload_to = 'images/')
caption = models.TextField()
alt = models.CharField(max_length = 20, default = "an image")
text = models.CharField(max_length = 5, null=True)
admin
from django.contrib import admin
from main.models import Slider
class SliderAdmin(admin.ModelAdmin):
list_display = ('image','alt','caption','text')
admin.site.register(Slider,SliderAdmin)