我在我的" test"中为我的博客应用创建了ImageField模型。使用sqllite在我的本地服务器上的django项目。
我有我的settings.py
MEDIA_ROOT = '/Users/me/Sites/python/djangotut/media/'
MEDIA_ROOT_URL = 'http://127.0.0.1:8000/media/images/photos/'
和我的博客/ models.py
photo = models.ImageField(upload_to='images/photos/')
但问题是我的blog.urls.py我不知道如何添加网址来处理文档https://docs.djangoproject.com/en/1.6/howto/static-files/#serving-files-uploaded-by-a-user-during-development
中的模式from django.conf.urls import url
from django.conf.urls.static import static
from .views import index, post
urlpatterns = [
url(
regex=r'^$',
view=index,
name='blog-index'
),
url(
regex=r'^(?P<slug>[\w\-]+)/$',
view=post,
name='blog-detail'
),
]
此外,我已经阅读了有关为生产环境设置网址的信息。当您分发应用程序时。在这种情况下,我的网址需要看起来像什么?
答案 0 :(得分:0)
更新。看一下:https://docs.djangoproject.com/en/dev/ref/models/fields/。
models.py
UPLOAD_PATH = "photos"
photo = models.ImageField(upload_to=UPLOAD_PATH)
settings.py
from path import path
PROJECT_DIR = path(__file__).dirname().parent
MEDIA_ROOT = PROJECT_DIR / 'media'
MEDIA_URL = '/images/'
urls.py
from django.conf import settings
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
答案 1 :(得分:0)
我发现我所寻找的与你的答案非常相似。特别是设置中的MEDIA_ROOT和MEDIA_URL不是确切的URL。
settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls.py
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.conf import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^newsletter/', include('newsletter.urls')),
url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
models.py保持不变。