Accessing “Media” files in Django

Folder Setup: Your project root should be something like: /app1 /app2 /media /static /templates The media folder is supposed to hold things like images, downloads and other material that might be uploaded during normal use of the website (i.e. after development is finished) The static folder is supposed to hold all the … Read more

Using Heroku for Django Media Files

Reason Heroku runs your application on dynos and dynos go to sleep after 30 minutes if there is not request comes, this makes Heroku not preserve user upload media files between dynos restart. Solution Use Amazon S3 service for storing and serving media files + you can also serve static files from S3 also. for … Read more

Django – Cascade deletion in ManyToManyRelation

I think you are misunderstanding the nature of a ManyToMany relationship. You talk about “the corresponding BlogEntry” being deleted. But the whole point of a ManyToMany is that each BlogEntryRevision has multiple BlogEntries related to it. (And, of course, each BlogEntry has multiple BlogEntryRevisions, but you know that already.) From the names you have used, … Read more

Django, request.user is always Anonymous User

The request.user is set by the django.contrib.auth.middleware.AuthenticationMiddleware. Check django/contrib/auth/ class LazyUser(object): def __get__(self, request, obj_type=None): if not hasattr(request, ‘_cached_user’): from django.contrib.auth import get_user request._cached_user = get_user(request) return request._cached_user class AuthenticationMiddleware(object): def process_request(self, request): request.__class__.user = LazyUser() return None Then look at the get_user function in django/contrib/auth/ def get_user(request): from django.contrib.auth.models import AnonymousUser try: user_id = … Read more

Provide extra context to all views

You could use the template context processor: myapp/ from django.contrib.auth.models import User from myapp.models import Project def users_and_projects(request): return {‘all_users’: User.objects.all(), ‘all_projects’: Project.objects.all()} And then add this processor to the TEMPLATE_CONTEXT_PROCESSORS setting for Django version < 1.8: TEMPLATE_CONTEXT_PROCESSORS = ( … ‘myapp.context_processors.users_and_projects’, ) And for Django version >= 1.8 add it to the context_processors list … Read more

Getting ‘DatabaseOperations’ object has no attribute ‘geo_db_type’ error when doing a syncdb

The OP was using the GeoDjango buildpack, but in case anyone gets here using Geo buildpack and dj_database_url like I was, in don’t forget the last line: import dj_database_url DATABASES[‘default’] = dj_database_url.config() DATABASES[‘default’][‘ENGINE’] = ‘django.contrib.gis.db.backends.postgis’ UPDATE dj_database_url directly supports PostGIS. You can do without the last line in the code above if you can … Read more