Django static url auto prefixes

1.3k views Asked by At

I've seen quite alot of questions & answers on this topic, but none helped me. So, the problem is: none of static files are served till i manually add '/' before any adress in adresses in html. And i always get double static files folder prefix on adresses, like this: 'content/static/content/static/js/my.js'

The last problem, that i CANT get over, is this image cropping field isnt working, i assume becouse it cant get right image url, becouse of double prefix written above. I cant fix it, becouse the mechanism is written inside the https://github.com/jonasundderwolf/django-image-cropping. Is there a way to change this django behavior? In the redactor field i had same problem, but i fixed it with manually overwriting save() method, changing the url of uploaded images so they begin to be displayed, but it doesnt work automatically and i cant understand why. Help please.

I tried {% static 'some_file' %}, sometimes its working, sometimes not, so its not a solution.

So, my django project structure is this:

  1. project
    • content
      • templates
      • static
      • etc
    • project

project/urls.py:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'', include ('content.urls')),
    url(r'^redactor/', include('redactor.urls')),
]

from django.conf.urls import patterns
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns += staticfiles_urlpatterns()

settings.py:

INSTALLED_APPS = (
    'content',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'redactor',
    'pure_pagination',
    'django_cleanup',
    'image_cropping',
    'easy_thumbnails',
)

from easy_thumbnails.conf import Settings as thumbnail_settings
THUMBNAIL_PROCESSORS = (
    'image_cropping.thumbnail_processors.crop_corners',
) + thumbnail_settings.THUMBNAIL_PROCESSORS

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

STATIC_URL = '/content/static/'

STATICFILES_FINDERS = ( 
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(
        os.path.dirname(__file__),
    ),
    "C:/Work/Snejana/magazine31.01.2016/content/static/upload_images",
    "C:/Work/Snejana/magazine31.01.2016/content/static",
    "C:/Work/Snejana/magazine31.01.2016",
)

one model from content/models.py:

class Author(models.Model):

    class Meta:
        verbose_name = 'Автора'
        verbose_name_plural = 'Авторы'

    author_name              = models.CharField(max_length=200, verbose_name=u'Имя автора')
    regalias                 = models.CharField(max_length=200, blank=True, verbose_name=u'Регалии')
    description              = RedactorField(blank=True,
                                             verbose_name=u'Описание автора',
                                             redactor_options={'lang': 'en', 'focus': 'true'},
                                             upload_to='content/static/upload_images/',
                                             allow_file_upload=True,
                                             allow_image_upload=True)
    photo_full               = models.ImageField(upload_to="content/static/upload_images/",
                                                 verbose_name=u'Фото',
                                                 default=None,
                                                 blank=True,
                                                 null=True)
    photo_thumbnail          = models.ImageField(upload_to="content/static/upload_images/",
                                                 verbose_name=u'Фото маленькое',
                                                 default=None,
                                                 blank=True,
                                                 null=True)
    publications_of_author   = models.ForeignKey('Publication', blank=True, null=True, verbose_name=u'Публикации автора')
    cropping1 = ImageRatioField('photo_full', '600x400')
    cropping2 = ImageRatioField('photo_thumbnail', '100x80')

And last one, admin.py:

class AuthorAdmin(ImageCroppingMixin, admin.ModelAdmin):
    fieldsets = [
        ('Информация об авторе', {'fields': ['author_name', 'description', 'photo_full', 'photo_thumbnail','cropping1', 'regalias'], 'classes': ['collapse']}),
    ]
    readonly_fields = ('photo_thumbnail',)
    inlines = [AuthorsPublicationsInline]

    class Media:
        js = ['http://code.jquery.com/jquery-latest.min.js', 'js/collapsed_stacked_inlines.js',]
1

There are 1 answers

3
Daniel Roseman On

Your upload_to attributes should not include the prefix; they are relative to MEDIA_ROOT.

photo_full = models.ImageField(upload_to="upload_images/", ...)