'NoneType'对象没有属性'endswith'、django、python

时间:2022-01-07 07:56:54

My project structure is something like:

我的项目结构是这样的:

  • /blog1 (Name of project)
    • /blog1/media/blog1/media
    • / blog1 /媒体/ blog1 /媒体
    • /blog (Name of app)
      • /blog/media
      • /博客/媒体
      • /static
      • /静态
      • /media
      • /媒体
    • /博客(app名称)/博客/媒体/静态/媒体。
    • /static/templates
    • /静态模板
  • /blog1(项目名称)/blog1/media/blog1/media /blog (app名称)/blog/media /static/ media/ static/template

I have uploaded a photo through django-admin panel , but when i view the image through the view page source from browser , it shows an error :

我已经通过django-admin面板上传了一张照片,但是当我通过浏览器的view page source查看图片时,它显示了一个错误:

AttributeError at /blog/view/blog1/media/high-tech-snapshot_3.jpg
'NoneType' object has no attribute 'endswith'

Traceback:

回溯:

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'

Here are my directories in settings.py :

这是我在设置中的目录。py:

STATIC_URL = '/static/'
MEDIA_URL = '/media/blog1/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'media')

Here are my models :

以下是我的模型:

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

Here is my views.py:

这是我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)

    })

My admin.py:

我的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:

我的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'),
)   

and my view_post.html:

和我view_post.html:

{% block content %}
<h2>{{ post.title }}</h2>
<p>{{ post.body }}</p>
<img src="{{ post.photo }}"/>
 {% endblock %}   

I am sure i am doing some silly mistake , if anyone knows please help me .

我确信我犯了一个愚蠢的错误,如果有人知道请帮助我。

Thanks in advance, and sorry for bad naming of directories , i am new to django and don't know how to list directories in right way.

预先感谢,抱歉对目录命名不好,我是django的新手,不知道如何以正确的方式列出目录。

1 个解决方案

#1


3  

I solved similar problem by add this in settings.py

我通过在settings.py中添加这个来解决类似的问题

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

and also maybe you need to run command

也许你还需要运行命令

python manage.py collectstatic

Hope this helps

希望这有助于

#1


3  

I solved similar problem by add this in settings.py

我通过在settings.py中添加这个来解决类似的问题

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

and also maybe you need to run command

也许你还需要运行命令

python manage.py collectstatic

Hope this helps

希望这有助于