Rackspace Cloudfiles and Django Cumulus

594 Views Asked by At

From the past 2 days i have literally lost my patience trying to make Cloudfiles work for my project(using cumulus). Here are some of the issues:

1.) Sometimes when i upload any photo using admin(creating a model and registering with admin)... the photo looks like its uploaded but when i try to either access it using a view function by Photo.objects.all() or even going to the cloudfiles control panel...the image simply doesnt open up. I get a resource not found. I check and double check if the region(chicago is default for me) settings is screwing with me....but i don't think so.

2.) I have used collectstatic method to successfully collect all static files in a container and i am able to successfully serve them. infact when i click the link(say for example) - http://ed770b871265201bf471-14f03984d90730040890dd30a2d85248.r68.cf2.rackcdn.com/admin/css/base.css

I am able to see the results and i am sure u can see it too.

But when i am trying to use them by STATIC_URL in my templates - The admin pages don't have their css working but my login/home pages are perfectly being rendered with my styles.

Here are my settings file/my view functions and anything that is important -

STATIC_ROOT = ''
STATIC_URL = 'http://ed770b871265201bf471-14f03984d90730040890dd30a2d85248.r68.cf2.rackcdn.com/'
STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR,'static'),
)

CUMULUS = {
    'USERNAME': '***',
    'API_KEY': '****',
    'CONTAINER': 'photos',
    'STATIC_CONTAINER':'static',
    'SERVICENET': False, # whether to use rackspace's internal private network
    'FILTER_LIST': [],
    'TIMEOUT' : 60
}
DEFAULT_FILE_STORAGE = 'cumulus.storage.CloudFilesStorage'
STATICFILES_STORAGE = 'cumulus.storage.CloudFilesStaticStorage'

The model file part

class Photo(models.Model):
    username = models.ForeignKey(User)
    image = models.ImageField(upload_to='photos')
    alt_text = models.CharField(max_length=255)

admin.site.register(Photo)
2

There are 2 best solutions below

1
On

This is the view function as you requested kyle.

def profile_detail(request):
        if request.user.is_authenticated():
            username = request.user.get_username()
            # userid = User.objects.get(username=username).values('__id')
            userdetails = User.objects.filter(username=username)
            photo = Photo.objects.get(username=request.user.id)
            return render_to_response('profile_detail.html',{'userdetails':userdetails,'username':username,'photo':photo},
                                        context_instance=RequestContext(request))

and the template for profile_detail -

{% extends 'profile.html' %}

{% load bootstrap_toolkit %}

{% block content %}

    <img src="{{ photo.image.url }}" alt="{{ photo.alt_text }}" />
    <br>
    <p>{{ user.first_name }}</p>
    <p>{{ user.last_name }}</p>
    <p>{{ user.email }}</p>

{% endblock %}

I just now checked that i can view the image(No idea how) on the hosted site(production) but still can't do it in my dev environment.

Kyle can you please check if your testaccount has a picture of penguins in the 'MYProfile' page? :) Thanks for looking into it :)

2
On

The images are there, but possibly not where you expected them.

Link on your current site:

http://d12df125d01b8a258a3a-8112fdc02f7d385b44f56eb9e899d81c.r88.cf2.rackcdn.com/photos/Penguins.jpg

Where the image/file actually is:

http://d12df125d01b8a258a3a-8112fdc02f7d385b44f56eb9e899d81c.r88.cf2.rackcdn.com/photos%5CPenguins.jpg

The %5C is a \ rather than a /. This makes a difference as these are keys (key being the "path" and value being the file). You may want to check on how these were uploaded, and possibly normalize them to regular slashes (were these uploaded while you were on a Windows machine?).