2014-03-25 32 views
0

我有一個簡單的Django管理頁面,可以將圖像上傳到產品說明。一切工作,直到我試圖通過點擊產品信息中的路徑來查看圖像。我得到這個錯誤:查看上傳到Django的圖像Admin

Page not found (404) 
Request URL: http://0.0.0.0:6666/the_image.jpg 

我猜我需要在urls.py中聲明一些東西,但我不知道從哪裏開始。我也試着改變settings.py我的媒體文件的路徑,但如果我更改爲 '/'

model.py

class Image(models.Model): 
    product_image = models.ForeignKey(Product) 
    image = models.ImageField(upload_to='/') 

settings.py

MEDIA_ROOT = '/' 
MEDIA_URL = '/' 

任何其他我總是錯誤admin.py

class InlineImage(admin.TabularInline): 
    model = Image 

class ProductAdmin(admin.ModelAdmin): 
    inlines = [InlineImage] 
+0

你只是使用的Django開發服務器?你有urls.py安裝程序來提供靜態文件嗎? –

+0

我想我使用的是開發服務器...「manage.py runserver 0.0.0.0:6666」是我用它來啓動它。而且,不,我不會,現在我會研究這個。 – AllTheTime

+0

查看https://docs.djangoproject.com/en/dev/ref/settings/#static-files 和https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/# module-django.contrib.staticfiles –

回答

1

文檔是在這裏https://docs.djangoproject.com/en/dev/ref/settings/#media-root

您需要設置MEDIA_ROOT和MEDIA_URL在你的設置文件中像這樣

MEDIA_ROOT = /var/www/example.com/media/ 
MEDIA_URL = /media 

和你upload_to大概應該是型號名稱或東西來標識它。

image = models.ImageField(upload_to='image') 

然後該鏈接應指向的/media/image/NAME_OF_IMAGE.png

您還需要有urls.py設置到服務器的媒體文件。對於生產你想在nginx中做一個別名。見https://docs.djangoproject.com/en/dev/howto/static-files/#serving-files-uploaded-by-a-user-during-development

它說:

from django.conf import settings 
from django.conf.urls.static import static 

urlpatterns = patterns('', 
    # ... the rest of your URLconf goes here ... 
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 
+0

非常感謝您的幫助!我剛剛完成了Django教程。超越它有很多微小的細節可以得到正確的結果,它可能有點壓倒性。另外,如果你不介意,一旦將我的應用程序移動到實際的服務器上,這種設置會如何改變? – AllTheTime

+0

我會推薦uwsgi和nginx。 uwsgi可能有點壓倒性,但與它合作非常好。這裏有一個教程http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html,示例nginx config具有媒體和靜態的別名 –