0

I'm been working on this Photo Organizer and Sharing App Part I at http://lightbird.net/dbe/photo.html. I'm trying to add a picture and when I do . I get this error.

 Page not found (404)
Request Method: GET 
Request URL: http://donkey123.pythonanywhere.com/admin/photo/image/1/images/Penguins_7.jpg/ 

image object with primary key u'1/images/Penguins_7.jpg' does not exist.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page. 

My Models is

from django.db import models
from django.contrib.auth.models import User
from django.contrib import admin
from string import join
import os
from PIL import Image as PImage
from mysite.settings import MEDIA_ROOT


class Album(models.Model):
    title = models.CharField(max_length=60)
    public = models.BooleanField(default=False)
    def __unicode__(self):
        return self.title

class Tag(models.Model):
    tag = models.CharField(max_length=50)
    def __unicode__(self):
        return self.tag

class Image(models.Model):
    title = models.CharField(max_length=60, blank=True, null=True)
    image = models.FileField(upload_to="images/")
    tags = models.ManyToManyField(Tag, blank=True)
    albums = models.ManyToManyField(Album, blank=True)
    created = models.DateTimeField(auto_now_add=True)
    rating = models.IntegerField(default=50)
    width = models.IntegerField(blank=True, null=True)
    height = models.IntegerField(blank=True, null=True)
    user = models.ForeignKey(User, null=True, blank=True)

    def __unicode__(self):
        return self.image.name
    def save(self, *args, **kwargs):
        """Save image dimensions."""
        super(Image, self).save(*args, **kwargs)
    im = PImage.open(os.path.join(MEDIA_ROOT, self.image.name))
    self.width, self.height = im.size
    super(Image, self).save(*args, ** kwargs)
def size(self):
    """Image size."""
    return "%s x %s" % (self.width, self.height)

def __unicode__(self):
    return self.image.name

def tags_(self):
    lst = [x[1] for x in self.tags.values_list()]
    return str(join(lst, ', '))

def albums_(self):
    lst = [x[1] for x in self.albums.values_list()]
    return str(join(lst, ', '))

def thumbnail(self):
    return """<a href="/media/%s"><img border="0" alt="" src="/media/%s" height="40" /></a>""" % (
                                                                (self.image.name, self.image.name))
thumbnail.allow_tags = True





class AlbumAdmin(admin.ModelAdmin):
    search_fields = ["title"]
    list_display = ["title"]

class TagAdmin(admin.ModelAdmin):
    list_display = ["tag"]

class ImageAdmin(admin.ModelAdmin):
    search_fields = ["title"]
    list_display =     ["__unicode__", "title", "user", "rating", "size", "tags_", "albums_","thumbnail","created"]
    list_filter = ["tags", "albums","user"]

def save_model(self, request, obj, form, change):
    obj.user = request.user
    obj.save()

I think My Models,py is fine but I think the problem lay at the MEDIA_ROOT= and MEDIA URL=

My MEDIA_ROOT = '/home/donkey123/mysite/photo'

and my MEDIA_URL is "" which is blank.

I don't know what to put for my MEDIA_URL which is the problem.

Thanks in advance

kartheek
  • 6,434
  • 3
  • 42
  • 41
supersheep1
  • 721
  • 2
  • 7
  • 17

4 Answers4

1

In your model (note there is no need for the slash at the end):

image = models.FileField(upload_to="images")

That means these images will go in {media folder}/images/

In settings.py:

MEDIA_ROOT = os.path.join(os.path.dirname(__file__), '../../web/media').replace('\\','/')

That means the media folder is: project_root/web/media

So the images from this model will go in: project_root/web/media/images

The stuffs above define where the images are saved on the filesystems. Now what you want to do is to define where they will be accessed via HTTP.

In settings.py define the url pointing to the media folder:

MEDIA_URL = '/media/'

Then the images from this model will be in:

http://my-url/media/images/image_name.jpg

Tests these settings without your model/library first with one image. Then read the following:

To reply to your specific questions:

My MEDIA_ROOT = '/home/donkey123/mystic/photo' => see my example, the MEDIA_ROOT should be relative to your settings.py file so it will work when going live, on another dev machine, etc.

and my MEDIA_URL= is "" which is blank. => that should be '/media/' because in the model you are doing this:

def thumbnail(self):
    return """<a href="/media/%s">

Hope that helps

François Constant
  • 5,531
  • 1
  • 33
  • 39
0

settings.py

import os

PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..')
SITE_ROOT = PROJECT_ROOT

MEDIA_ROOT = os.path.join(SITE_ROOT, 'media')
MEDIA_URL = '/media/'
catherine
  • 22,492
  • 12
  • 61
  • 85
  • that is the correct code for your path, you have to update the way you save your data. Are you saving your image through admin panel? – catherine Feb 11 '13 at 05:48
0

Problem is with the regular expression in your urls.py, which is taking everything after 1 and passing it in as the primary key.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
-1

I think, When any web page is removed or moved then 404 happens. Otherwise your hosting provider is not good.

j0k
  • 22,600
  • 28
  • 79
  • 90