有问题。 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)
答案 0 :(得分:0)
请参阅https://docs.djangoproject.com/en/1.8/howto/static-files/
将STATIC_URL添加到settings.py