English 中文(简体)
无法在简便的django现场展示图像
原标题:Cannot get images to display in simple django site
  • 时间:2010-01-27 16:54:19
  •  标签:
  • django
  • image

我有一个非常简单的幻灯塔网站,但我难以看到我在行政小组上传来的图像。

www.un.org/Depts/DGACM/index_french.htm

# 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/ 

页: 1 looks:

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}),
)

页: 1 looks:

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

我能够成功地上载行政接口的照片,并储存在<条码>上。 但是,如果我去做,例如http://127.0.0.1:8000/media/truitjes/DSC00068.J。 我发现了一个错误:Page未发现: /media/truitjes/DSC00068.JPG。 Same for http://127.0.0.1:8000/media/truitjes and `“ http://127.0.0.1:8000/media/gives 驳回申请:

最佳回答

改变你的媒体或行政结构。

他们不能有同样的价值。 如果是的话,ADMIN_MEDIA_PREFIX就具有优先地位。 这意味着,如果你试图查阅<条码>http://127.0.0.1:8000/media,你将试图查阅行政媒体的夹。

Either change the ADMIN_MEDIA_PREFIX:

ADMIN_MEDIA_PREFIX =  /admin-media/ 

或修改<代码> 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}),
问题回答

在Django1.3,如果你在发展过程中重新尝试展示形象的话:

  1. Create a folder titled "static" in your app s directory
  2. Place your image in your static folder
  3. Make sure STATIC_URL in your settings.py is set to /static/ - this should be default
  4. 页: 1 py:

    from django.contrib.staticfiles.urls import staticfiles_urlpatterns
    #..rest of url.py config...
    urlpatterns += staticfiles_urlpatterns()
    
  5. 在您的模板中,如果你想要了解你的情况,就使用这一模板的标签:

    <img src="{{ STATIC_URL }}pic.png" />
    

看看在发展方面的统计档案:https://docs.djangoproject.com/en/1.3/howto/static-files/#staticfiles-in-templates





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签