Object "NoneType" has no attribute "endswith", django, python

My project structure looks something like this:

  • / blog1 (project name)
    • / blog1 / media / blog1 / media
    • / blog (application name)
      • / blog / media
      • / static
      • /MEDIA
    • / static / templates

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

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:

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 are my 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:

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

      

and my view_post.html:

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

      

I'm sure I am making some silly mistake, if anyone knows please help me.

Thanks in advance, and sorry for the bad directory name, I'm new to django and don't know how to list directories correctly.

+3


source to share


1 answer


I solved a similar problem by adding this to 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 the command



python manage.py collectstatic

      

Hope it helps

+3


source







All Articles