找不到Django错误页面(404) [英] Django Error Page not found (404)
问题描述
我一直在 http://lightbird.net上从事此照片管理器和共享应用程序第一部分的工作。 /dbe/photo.html 。我正在尝试添加图片,当我添加时。我收到此错误。
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.
我的模型是
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()
我认为我的模型很好,但我认为问题出在他
MEDIA_ROOT =
和 MEDIA URL =
I think My Models,py is fine but I think the problem lay at the
MEDIA_ROOT=
and MEDIA URL=
我的 MEDIA_ROOT ='/ home / donkey123 / mysite / photo'
和我的 MEDIA_URL
是,为空。
我不知道要为我的 MEDIA_URL输入什么
这就是问题所在。
I don't know what to put for my MEDIA_URL
which is the problem.
预先感谢
推荐答案
在模型中(请注意,结尾不需要斜杠):
In your model (note there is no need for the slash at the end):
image = models.FileField(upload_to="images")
这意味着这些图像将放入 {media文件夹} / images /
That means these images will go in {media folder}/images/
在settings.py中:
In settings.py:
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), '../../web/media').replace('\\','/')
这意味着媒体文件夹为: project_root / web / media
That means the media folder is: project_root/web/media
因此该模型中的图像将进入: project_root / web / media / images
So the images from this model will go in: project_root/web/media/images
上面的内容定义了图像在文件系统上的保存位置。现在,您要做的就是定义通过HTTP访问它们的位置。
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.
在settings.py中,定义指向媒体文件夹的网址:
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:
回答您的特定问题:
我的MEDIA_ROOT ='/ home / donkey123 / mystic / photo'=>参见我的示例,MEDIA_ROOT应该相对于settings.py文件,以便在其他开发机器上运行时也可以正常工作。
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.
,而我的MEDIA_URL =为,为空。 =>应该是'/ media /',因为在模型中您正在执行以下操作:
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">
希望有帮助
这篇关于找不到Django错误页面(404)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!