Bruno All the help is much appreciated - its working now, and I need to spend time to understand all the changes.
On Nov 5, 5:53 pm, bruno desthuilliers <[email protected]> wrote: > On 3 nov, 16:02, derek <[email protected]> wrote: > > > Given that I am a "wet behind the ears" newbie, maybe I should not be > > attempting this, but... > > Attempts at thing we don't yet fully grasp can be a good way to > learn. > > > I need some simple and straightforward guidance on > > uploading and using images in a simple database application, which (so far) > > only uses the admin interface. > > > I working on my development machine (i.e. no Apache integration as yet). > > > The use case is not that complex. If I have an "Event" object, with which I > > want to associate a number of images (typically, photos taken by people at > > that event), then I also need an "EventImage" object, with the event.id as > > the foreign key. > > > So, the models look like: > > > class Event(models.Model): > > id = models.AutoField(primary_key=True) > > Hint : if you leave this previous line out, Django will automagically > add this very same field to your model !-) > > > > > date_time = models.DateTimeField() > > title = models.CharField(unique=True,max_length=250) > > > class EventImage(models.Model): > > id = models.AutoField(primary_key=True) > > caption = models.CharField(unique=True,max_length=10) > > title = models.CharField(max_length=250) > > event = models.ForeignKey(Event) > > image = models.ImageField(upload_to='photos') > > > What I have gathered so far, from browsing articles and snippets and Q&A, is > > that I also need: > > > 1.to add/change settings.py: > > > MEDIA_ROOT = '/home/blah/blah/mysite/media/' > > MEDIA_URL = 'http://127.0.0.1:8000/media/' > > ADMIN_MEDIA_PREFIX = '/media/' > > Using the same path component for both the static medias and the admin > media usually leads to unexpected results. Either change your static > medias component path to something else or - way simplier -, change > the ADMIN_MEDIA_PREFIX (here we canonically set it to 'admin-media'). > > > STATIC_DOC_ROOT = '/home/blah/blah/mysite/media/' > > > 2. add the following to the start of urls.py: > > > from django.conf import settings > > > 3. add the following to the end of urls.py: > > > if settings.DEBUG: > > urlpatterns += patterns('', > > (r'^static/(?P<path>.*)$', 'django.views.static.serve', > > {'document_root': settings.STATIC_DOC_ROOT}), > > ) > > This url pattern is not consistant with the your MEDIA_URL settings. A > good way to avoid this kind of problems is to follow the SPOT rule: > > ### in settings.py > import os > > PROJECT_PATH = os.path.dirname(os.path.abspath(__file__)) > SITE_URL = "http://127.0.0.1:8000" > > MEDIA_DIR = "media" > MEDIA_ROOT = os.path.join(PROJECT_PATH, MEDIA_DIR) > MEDIA_URL = "%s/%s/" % (SITE_URL, MEDIA_DIR) > > ADMIN_MEDIA_PREFIX = '/admin-media/' > > # decouple this from the DEBUG flag - you may want to > # set DEBUG to true in preproduction > # (ie, running behind apache or another web server) > DEV_SERVER = True > > ### in urls.py > from django.conf import settings > > # urls here... > > if settings.DEV_SERVER: > urlpatterns += patterns('', > (r'^%s/(?P<path>.*)$' % setting.MEDIA_DIR, > 'django.views.static.serve', > {'document_root': settings.MEDIA_ROOT}), > ) > > > (I have also tried variations on the above, such as: > > (r'^media/(?P<path>.*)$', 'django.views.static.serve', > > which may have worked, minus the conflict between MEDIA_URL and > ADMIN_MEDIA_PREFIX > > > and > > (r'%s(?P<path>.*)' % settings.MEDIA_URL[1:], > > MEDIA_URL is a string, so MEDIA_URL[1:] is the same string minus the > first character, ie, in your case : "ttp://127.0.0.1:8000/media/". > Probably not what you expected !-) > > > 'django.views.static.serve', > > {'document_root': settings.MEDIA_ROOT}), > > ) > > > but in all cases, although the file does get uploaded and appears in the > > '/home/blah/blah/mysite/media/photos/' directory, when I click on the link - > > which is shown as > > >http://127.0.0.1:8000/media/photos/test.jpg > > > I get a "Page not found: /media/photos/test.jpg" > > > This seems like a simple thing to fix - but not for me? > > cf above. > > > Any help to fix this is appreciated, as is any guidance on what will need to > > change once this project needs to run under Apache... > > If you're careful to use the "medias" context processor and > {{ MEDIA_URL }} in your templates, you shouldn't have to do much more > than update your settings.py appropriately. But anyway - first fix > your development setup. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---

