我的项目结构类似于:
我已经通过django-admin面板上传了一张照片,但是当我通过浏览器中的查看页面源查看图片时,显示错误:
AttributeError at /blog/view/blog1/media/high-tech-snapshot_3.jpg
'NoneType' object has no attribute 'endswith'
回溯:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/view/blog1/media/high-tech-snapshot_3.jpg
Django Version: 1.6.5
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
'embed_video')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/static.py" in serve
49. fullpath = os.path.join(document_root, newpath)
File "/usr/lib/python2.7/posixpath.py" in join
77. elif path == '' or path.endswith('/'):
Exception Type: AttributeError at /blog/view/blog1/media/high-tech-snapshot_3.jpg
Exception Value: 'NoneType' object has no attribute 'endswith'
以下是settings.py中的目录:
STATIC_URL = '/static/'
MEDIA_URL = '/media/blog1/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'media')
以下是我的模特:
from django.db import models
from django.db.models import permalink
from embed_video.fields import EmbedVideoField
class Blog(models.Model):
title = models.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=100, unique=True)
body = models.TextField()
posted = models.DateTimeField(db_index=True, auto_now_add=True)
category = models.ForeignKey('blog.Category')
video = EmbedVideoField( blank=True)
photo = models.ImageField(upload_to="blog1/media/", blank=True)
def __unicode__(self):
return '%s' % self.title
@permalink
def get_absolute_url(self):
return ('view_blog_post', None, { 'slug': self.slug })
这是我的views.py:
def index(request):
return render_to_response('index.html', {
'categories': Category.objects.all(),
'posts': Blog.objects.all()[:6]
})
def view_post(request, slug):
return render_to_response('view_post.html', {
'post': get_object_or_404(Blog, slug=slug)
})
我的admin.py:
from django.contrib import admin
from blog.models import Blog
from embed_video.admin import AdminVideoMixin
class BlogAdmin(admin.ModelAdmin):
exclude = ['posted']
prepopulated_fields = {'slug': ('title',)}
admin.site.register(Blog, BlogAdmin)
my urls.py:
from django.conf.urls import patterns, include, url
import os
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'blog1.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'blog.views.index'),
url(
r'^blog/view/(?P<slug>[^\.]+).html',
'blog.views.view_post',
name='view_blog_post'),
url(r'^About/$', 'blog.views.about', name='about'),
url(
r'^blog/category/(?P<slug>[^\.]+).html',
'blog.views.view_category',
name='view_blog_category'),
url(r'^blog/view/blog1/media/(.*)$', 'django.views.static.serve',name ='media_url'),
)
和我的view_post.html:
{% block content %}
<h2>{{ post.title }}</h2>
<p>{{ post.body }}</p>
<img src="{{ post.photo }}"/>
{% endblock %}
我确信我在做一些愚蠢的错误,如果有人知道请帮助我。
提前致谢,对于错误的目录命名感到抱歉,我是django的新手,并且不知道如何以正确的方式列出目录。 < / p>
答案 0 :(得分:3)
我在settings.py
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
也许你需要运行命令
python manage.py collectstatic
希望这有帮助