[Django]-Django Multiple Authentication Backend for one project

49👍 ✅ You can have multiple authentication backends. Just set the AUTHENTICATION_BACKENDS in settings.py of your Django project to list the backend implementations you want to use. For example I often use a combination of OpenID authentication and the standard Django authentication, like this in my settings.py: AUTHENTICATION_BACKENDS = ( ‘django.contrib.auth.backends.ModelBackend’, ‘django_openid_auth.auth.OpenIDBackend’, ) In this … Read more

[Django]-Django REST Framework (DRF): Set current user id as field value

8👍 ✅ In DRF version prior 3 field must be declader with allow_null=True and default=None. DRF don’t run checking fields without this params. Result code: class NewsReadSerializer(serializers.ModelSerializer): “”” Serializer only for reading. author field serialized with other custom serializer “”” author = UserMiniSerializer() class Meta: model = NewsModel fields = (‘id’, ‘title’, ‘announce’, ‘comments_count’, ‘reviews’, … Read more

[Django]-Annotate a queryset with the average date difference? (django)

61👍 Just an update. In Django >= 1.8 it is possible to do: from django.db.models import F, ExpressionWrapper, fields duration = ExpressionWrapper(F(‘date_end’) – F(‘date_start’), output_field=fields.DurationField()) events_with_duration = Event.objects.annotate(duration=duration) after which you can run queries like: events_with_duration.filter(duration__gt=timedelta(days=10)) 👤ryuusenshi [Django]-AccessDenied when calling the CreateMultipartUpload operation in Django using django-storages and boto3 1👍 You’ll need to create a … Read more

[Django]-AccessDenied when calling the CreateMultipartUpload operation in Django using django-storages and boto3

47👍 ✅ It turns out that I had to specify a policy that adds permission to use any object /* under a bucket. Before … “Resource”: [ “arn:aws:s3:::www.xyz.com” ] … After … “Resource”: [ “arn:aws:s3:::www.xyz.com/*” ] … 👤Fahad Alrashed [Django]-In django, how do I sort a model on a field and then get the last … Read more

[Django]-In django, how do I sort a model on a field and then get the last item?

34👍 ✅ obj = Edition.objects.latest(‘pub_date’) You can also simplify things by putting get_latest_by in the model’s Meta, then you’ll be able to do obj = Edition.objects.latest() See the docs for more info. You’ll probably also want to set the ordering Meta option. 👤Harley Holcombe [Django]-Sending post data from angularjs to django as JSON and not … Read more

[Django]-Sending post data from angularjs to django as JSON and not as raw content

44👍 ✅ When calling ajax, you recieve encoded json string in request body, so you need to decode it using python’s json module to get python dict: json.loads(request.body) 👤mariodev [Django]-CORS error while consuming calling REST API with React 14👍 In my case works something like $http({ url: ‘/url/’, method: “POST”, data: $.param(params), headers: { ‘Content-Type’: … Read more

[Django]-CORS error while consuming calling REST API with React

31👍 ✅ Please Note: This solution is not for production configuration. This is merely a workaround for easier setup while development. Please refrain from using this in production configuration. Install django-cors-headers through pip install django-cors-headers Then, add in installed apps ‘corsheaders’. Add the setting, CORS_ORIGIN_ALLOW_ALL = True and, ALLOWED_HOSTS = [‘*’] This should do the … Read more

[Django]-Nginx doesn't serve static

62👍 ✅ How is your directory setup? Do you have a folder static in /home/user/www/oil/oil_database/static_files? In that case, the directive should look like this (note the trailing slash in /static/): location /static/ { autoindex on; root /home/user/www/oil/oil_database/static_files; } If you want to map the path /home/user/www/oil/oil_database/static_files to the URL /static/, you have to either rename … Read more

[Django]-Passing STATIC_URL to file javascript with django

21👍 ✅ django-compressor lets you do this as well as optimize your site by condensing all of your required JS or CSS into one file and optimizing file size. UPDATE: By default, compressor will convert relative urls into absolute urls using STATIC_URL. If you download the development version, it comes with a django template engine … Read more

[Django]-Django apps aren't loaded yet when using asgi

12👍 ✅ Before using Django infrastructure in standalone application you should always do django.setup() So, your asgi.py should be like import os from channels.routing import get_default_application os.environ.setdefault(“DJANGO_SETTINGS_MODULE”, “my_project.settings”) django.setup() application = get_default_application() 👤mingaleg [Django]-Why won't Django use IPython? 48👍 For anyone using Django > 3.0 refer to link below. It worked for me https://github.com/django/channels/issues/1564#issuecomment-722354397 all … Read more