Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django 1.9+, django-storages to s3 , always access denied with collect static
I never had this problem with previous projects and it's driving me crazy. With the aws CLI I uploaded some static files (as test for accesses etc) but when I configure my settings (following the 19287 examples on the net AND my previous project) I keep having: botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the PutObject operation: Access Denied First of all, the bucket permissions: { "Version": "2017-01-01", "Statement": [ { "Effect": "Allow", "Action": [ "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::my-bucket" ] }, { "Effect": "Allow", "Action": [ "s3:DeleteObject", "s3:GetObject", "s3:PutObject", "s3:RestoreObject" ], "Resource": [ "arn:aws:s3:::my-bucket/*", "arn:aws:s3:::my-bucket" ] } ] } The back: #AWS ACCESSES AWS_STORAGE_BUCKET_NAME = os.environ.get('DJANGO_AWS_STORAGE_BUCKET_NAME') AWS_ACCESS_KEY_ID = os.environ.get('DJANGO_AWS_ACCESS_KEY_ID') AWS_SECRET_ACCESS_KEY = os.environ.get('DJANGO_AWS_SECRET_ACCESS_KEY') AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME STATICFILES_LOCATION = 'static' MEDIAFILES_LOCATION = 'media' DEFAULT_FILE_STORAGE = 'project.storage.StaticStorage' STATICFILES_STORAGE = 'project.storage.MediaStorage' STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION) MEDIA_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION) And the storage: from django.conf import settings from storages.backends.s3boto3 import S3Boto3Storage class StaticStorage(S3Boto3Storage): location = settings.STATICFILES_LOCATION class MediaStorage(S3Boto3Storage): location = settings.MEDIAFILES_LOCATION So I'm probably too much into it and I can't see a silly mistake or is there a step missing? (I checked the values from the env, it's correctly loaded, debug = False) Many thanks! -
Python/django import nightmare (unique case)
I have a project structure like this, myAPI/ <--- announcements/ models.py views.py tests.py <--- tests/ <---- models.py views.py myAPI/ <--- settings.py .... Now I am not able to import my tests(django app) to my announcements app. announcements/models.py from tests.models import Test <--- Error!! because it looking for a class in django/announcements/tests.py file I also tried: from myAPI.tests.models import Test <--- Error!! It looks for a module in myAPI folder(with settings.py file) which obviously throws an error. Any help with this import would be appreciated! -
Can't add extra argument to Python(Django) function call
I need to serialize data with a serialiser and also I have a file for saving but I can't path the extra varible with file to my serializer issue_dict = request.data.get('issue') file = request.data.get('file') This is working fine: serializer = WriteIssueSerializer(data=issue_dict, context=self.get_serializer_context()) This is what I'd like to get, but it says "got an unexpected keyword argument 'file'" : serializer = WriteIssueSerializer(data=issue_dict, file=file, context=self.get_serializer_context()) I understand that inside the serializer I should define variable "file", so look at this serializer: class WriteIssueSerializer(serializers.ModelSerializer): notes = IssueNoteSerializer(many=True) def create(self, val): issue_dict = val.get('issue') # issue_dict['assigned_to'] = issue_dict['assigned_to']['id'] # issue_dict['reported_by'] = issue_dict['reported_by']['id'] assigned_to_id = issue_dict.pop('assigned_to').id reported_by_id = issue_dict.pop('reported_by').id notes_info = issue_dict.pop('notes') # print(validated_data.pop('file')) issue = Issue.objects.create(assigned_to_id=assigned_to_id, reported_by_id=reported_by_id, **issue_dict) for note_info in notes_info: note = IssueNote.objects.create(**note_info) note.issue = issue It's obvious that changing from def create(self, val): to def create(self, val, file): will fix my error, but not, the error is still the same -
xhtml2pdf background-image is not scaling
I'm generating an pdf with xhtml2pdf library, and I have a strange situation regarding background image. Image is displayed but it does not fit the @page, it's setup something like this @page { size: {% block page_size %}A4 {% endblock %}; background: url("{% static 'folder/background.png' %}") no-repeat center center fixed; @frame content_farme { margin-top: 70pt; margin-bottom: 60pt; margin-left: 30pt; margin-right: 30pt; @bottom-right { font-size: 10px; margin: 40pt -10pt 0 0; {% block page_foot_extra_styles %}{% endblock %} content: {% block page_foot %}"Page " counter(page) " of " counter(pages){% endblock %}; } @top-left { font-size: 10px; font-weight: bold; {% block page_header_extra_styles %}{% endblock %} content: {% block page_head %}{% endblock %}; } } } I want it to be placed in my @page, I've tried everything, as I understand standard css property are not working like it should, so how can I scale this background in xhtml2pdf there is a nice test file in docs but I didn't find it useful, did someone have this kind off issues, and how did you solved it. -
Best method to get a models fields all foreign key id's
Lets say i have a model: class Profile(models.Model): user = models.ForeignKey(user) I want to get that models all user foreign keys normally i can use, pro = Profile.objects.all() ids = [] for pros in pro: ids.append(pros.user_id) id there a better method? -
Haystack SearchFieldError : The model 'None' combined with model_attr <attr_name> returned None
On running rebuild_index, objects from other app are getting indexed but on one app SearchFieldError is happening. This is my models.py for the concerned app : class Doctor(models.Model): SPECIALTY_CHOICES = ( ('Pediatric Physiotherapist', 'Pediatric Physiotherapist') ) user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE) doctorName = models.CharField(max_length=100) doctorQual = models.CharField(max_length=500) doctorSpecial = models.CharField(choices=SPECIALTY_CHOICES, default='', max_length=50) doctorSummary = models.CharField(max_length=1000) doctorLocation = models.CharField(max_length=100, default=' ', null=True, blank=True) slug = models.SlugField(null=True, blank=True) city = models.CharField(max_length = 100, null=True, blank=True) def __str__(self): return self.doctorName # save method overridden to generate slug for each object def save(self, *args, **kwargs): self.slug = slugify(self.doctorName) super(Doctor, self).save(*args, **kwargs) def get_absolute_url(self): return "/doctor/doc_detail_page/%i" % self.id def get_summary(self): return self.doctorSummary class Meta: permissions = ( ("can_create", "yoyo"), ) And my search_indexes.py is : from haystack import indexes from .models import Doctor class DoctorIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) doctorName = indexes.CharField(model_attr='doctorName') doctorSummary = indexes.CharField(model_attr='doctorSummary') doctorSpecial = indexes.CharField(model_attr='doctorSpecial', faceted=True) doctorLocation = indexes.CharField(model_attr='doctorLocation',faceted=True) content_auto_doc = indexes.EdgeNgramField(model_attr='doctorName') # this field is for auto complete def get_model(self): return Doctor def index_queryset(self, using=None): return self.get_model().objects.all() These are the versions: elasticsearch==5.0.1 django-haystack==2.5.1 django--1.10 What am I doing wrong here. Please help!! -
Installing xapian for django error python too old but using python 2.7
I've installed xapian-core it's dependency xapian-bindings fails. Detail on how I install it are: cd ~/src wget http://oligarchy.co.uk/xapian/1.2.12/xapian-bindings-1.2.12.tar.gz tar zxf xapian-bindings-1.2.12.tar.gz cd xapian-bindings-1.2.12 ./configure --prefix=$HOME \ PYTHON=/usr/local/bin/python2.7 \ PYTHON_LIB=$HOME/lib/python2.7 \ --with-python --without-ruby \ --without-php --without-tcl During installation it fails and here's the last lines during the proccess checking /usr/bin/xapian-config works... yes checking whether to enable maintainer-specific portions of Makefiles... no checking for /usr/local/bin/python2.7... /usr/local/bin/python2.7 checking /usr/local/bin/python2.7 version... (too old) configure: error: Only python 2.3 or newer is supported ( is ) I'm running on a amazon server that has python 2.7 installed by default. -
Django, bulk insert of DIFFERENT objects?
I currently have a chat system set up like class Link(models.Model): user = models.ForeignKey(User, on_delete=models.PROTECT) chat = models.ForeignKey(Chat, on_delete=models.PROTECT) class Chat(models.Model): title = models.CharField(max_length=1000) class Message(models.Model): user = models.ForeignKey(User, on_delete=models.PROTECT) chat = models.ForeignKey(Chat, on_delete=models.PROTECT) text = models.CharField(max_length=1000) date = models.DateTimeField() There is a chat room that has attached to it multiple messages by different users, and each user is connected to a chat by a link (a link acts as a way to check if a user is "authorized" to be in the chat room) However, this presents me with the problem that each time a new chat room is created between 2 people, I have to insert 4 objects (2 links, 1 chat, 1 message) Is there a way to instead do all 4 saves in 1 call instead? -
Django DRF - How to do CSRF validation with token authentication
I am working on REST API using Django and Django Rest Framework. In the front-end I have AngularJs app. I have used Token authentication for APIs. This disables the CSRF checks. I want keep CSRF validation with REST API. How to achieve this ? How should I get CSRF Token value so that it can be set into the header of every POST request, using interceptor of my angular application. -
DRF nested serializer put very deep field in top level without intermediary layers
I have a legacy application that has model relations eight levels deep relative to the top entry point (A -> B -> C -> D -> E -> F -> G -> H), and F -> G -> H jumps over into Mongo. I know that I could write serializers for each level and reference each, as in the DRF documentation example here. Then my output would end up eight levels deep, which is not what I want. What I want is to summarize the single field from model H in the endpoint for A. I only need read-only access to the data in H. What I'm considering doing is implementing a custom serializers.RelatedField to B and using its to_representation() method to do the lookups. Is this the recommended approach? -
Django and direct database manipulation
I have a project where the client has to log in to the database and manually do the data entry there. Yes,direct access,don't ask why! What I want is when I apply the changes to the table,the created and modified field to be auto-copleted with current date and time. I am using MySQL. So I have created my model as so.. class Atlantis(models.Model): description = models.TextField('description', max_length=200 , default = None,null=True,blank=True) amount = models.IntegerField('Amount', default=0) quantity = models.IntegerField('Quantity', default=0) inserted = models.BooleanField(default=False) update_chk = models.BooleanField(default=False) created = models.DateTimeField(auto_now=True,auto_now_add=True) modified = models.DateTimeField(auto_now=True) def save(self, *args, **kwargs): ''' On save, update timestamps ''' if not self.id: self.created = timezone.now() self.modified = timezone.now() return super(Atlantis, self).save(*args, **kwargs) When I add data to the database the created field isn't being auto-completed with current timestamp. I tried to alter the table in the database and change the default setting for created field to CURRENT_TIMESTAMP,but there was an error. So I am asking, can what I am asking be done? -
Django REST Swagger: How to build SecurityDefinition in Swagger settings
I'm trying to build the Swagger settings for SecurityDefinition in order to get the following result in openapi.json: "securityDefinitions": { "UserSecurity": { "type": "apiKey", "in": "header", "name": "apikey" } }, "security": [{ "UserSecurity": [] }] In my settings.py I have the following settings: # Swagger settings SWAGGER_SETTINGS = { 'SECURITY_DEFINITIONS': { 'user_security': { 'type': 'apiKey', 'in': 'header', 'name': 'apikey' } }, 'security': [{ 'user_security': [] }] } The problem is that when I'm trying to get the openapi.json there is not presented the security dict. Is there any better way to describe it in my Swagger settings? -
Django- call to is_valid() appears to cause MultiValueDictKeyError
When trying to upload an image file to a form on one of my Django webpages, I can select the image by clicking 'Choose File' and selecting the file in the Dialog box that is displayed. However, when I then click the 'Upload' button, to upload the image to the project via the form, I get an error page which says: MultiValueDictKeyError at /projects/6215/upload-budget-pdf/ The 'Traceback' on the error page shows that the error is happening in the view: def upload_budget_pdfs(request, project_id): project = Project.objects.get(id=project_id) # budget_formset = BudgetUploadFormset(request.POST, request.FILES) drawing_formset = DrawingUploadFormset(request.POST, request.FILES, prefix="drawings") if drawing_formset.is_valid(): print 'Saving drawing_formset' print "Before", [b.id for b in project.budget_versions.all()] for drawing_form in drawing_formset: if drawing_form.instance.budget: print 'Instance', drawing_form.instance.budget drawing = drawing_form.save(commit=False) drawing.budget = drawing_form.instance.budget drawing.save() print drawing, [b.id for b in project.budget_versions.all()] else: print 'Drawing formset not valid.',drawing_formset.errors budget_formset = BudgetPresentationFormset(request.POST, request.FILES, instance=project, prefix="presentations") if budget_formset.is_valid() and budget_formset.has_changed(): updated_budget_presentations = budget_formset.save() elif budget_formset.has_changed(): print 'Budget formset not valid.',budget_formset.errors return HttpResponseRedirect(reverse('projects:concept', args=[project_id])) and the particular line it's complaining about is: if drawing_formset.is_valid(): As I understand, this error is usually caused by a call to request.POST? I am calling request.POST in the parameter where I define drawing_formset: drawing_formset = DrawingUploadFormset(request.POST, request.FILES, prefix="drawings") From what … -
string indices must be integers and no JSON objects could be decoded
before I used dict= json.loads(decoded_json) print(dict) in my code I was getting string indices must be integers.not after using json.loads I am getting No JSON object could be decoded. what to do? Views.py @ensure_csrf_cookie def register(request): csrf_token = get_token(request) board_detail = request.body print(board_detail) decoded_json = urllib.unquote(board_detail).decode('utf8') print (decoded_json) dict= json.loads(decoded_json) print(dict) boardIPReg = Registration.objects.all() print(boardIPReg) for boardIP in boardIPReg : if dict["ip_address"] == boardIP: print ("inside if") return HttpResponse(str(board_detail.ip_address)+""+str(board_detail.status)+""+str(board_detail.lastUpdateReceived)+""+str(board_detail.taskRunning)) print(str(board_detail.ip_address)+""+str(board_detail.status)) else: print("inside else") return HttpResponse("IP is not registered") return render(request, "devices/device_container.html", { "boardIPReg" :boardIPReg, }) return HttpResponse(board_detail) java script <script> function getCookie(c_name) { if (document.cookie.length > 0) { c_start = document.cookie.indexOf(c_name + "="); if (c_start != -1) { c_start = c_start + c_name.length + 1; c_end = document.cookie.indexOf(";", c_start); if (c_end == -1) c_end = document.cookie.length; return unescape(document.cookie.substring(c_start,c_end)); } } return ""; } $(function () { $.ajaxSetup({ headers: { "X-CSRFToken": getCookie("csrftoken") } }); }); function addip(){ console.log("hello"); var _ip = document.getElementById("tgtBoardIP"); console.log(_ip.value); $.ajax({ type:"POST", url:"/register/", data: { data : JSON.stringify({ ip_address : _ip.value, status : 'online', lastUpdateReceived : '08-01-2017' , taskRunning : '2G ID catcher' },null, '\t')}, contentType: 'application/json; charset=utf-8', success: function(data) { //console.log("Congrats you sent some data: " + data) ; // document.getElementById("board_block1").innerHTML='<object type="html" data="device.html"></object>'; console.log("Success"); // $('#device_container').html(data); }, … -
what is a mean of "*" when people can use import?
what is a mean of "" ? I cannot understand ,for example ,""of sound.effects import *. I think * cannot describe a folder of directory. -
How to read merged cell with xlrd in python?
I'm trying to read merged cells from sheet and unfortunately nothing is working for me. I'm in a situation where i want to read Row merges, column merges or may be combination of both. Any help will be appreciated. PS. I already tried sheets.merged_cells which returns blank array. Thank you. -
Image URL doesn't retrieve image
I load an image to my server side in django-admin with ImageField. My server is deployed to Heroku. I browse a .png image and search his path, I received it properly. After couple of minutes, the server doesn't retrieve the image. In the server logs, I get status 200 or 301, in both cases I don't get the image. Anyone have any idea to help me find a way to fix this situation? -
python-django: how to create user specific tables in django
I am creating a chat application on django and want to have separate tables for each chat i have. So that I don't have to search through a single tables containing all the chat messages. Just want to create a single model and separate tables for each user. -Thanks in advance -
Does celery task id change after redistribution
I have a Django model which has a column called celery_task_id. I am using RabbitMQ as the broker. There's a celery function called test_process which takes a model object as a parameter. Now I have the following lines of code which creates a celery task. def create_celery_task(): celery_task_id = test_celery.apply_async((model_obj,), eta='Future Datetime Object') model_obj.celery_task_id = celery_task_id model_obj.save() ---- ---- Now inside the celery function I am verifying if the task id is same as of the one stored in the DB or not. @app.task def test_celery(model_obj): if model_obj.celery_task_id == test_celery.request.id: ## Do something My problem is there are a lot of cases where I can see the task being received and succeeding in the log but not executing the code inside of if condition. Is it possible that celery task id changes after redistribution. Or are there any other reasons. -
Django error: django.db.utils.OperationalError: FATAL: Peer authentication failed for user
I'm setting up django on amazon web and I'm getting an issue connecting to the database. I created the database and checked to see it exists: postgres=# CREATE DATABASE kbuzz; ERROR: database "kbuzz" already exists and the user also exists postgres=# SELECT 1 FROM pg_roles WHERE rolname='kbuzz_web' postgres-# ; ?column? ---------- 1 (1 row) here's my database settings DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'kbuzz', # Or path to database file if using sqlite3. 'USER': 'kbuzz_web', # Not used with sqlite3. 'PASSWORD': '<hidden>', # Not used with sqlite3. 'HOST': 'localhost', # Set to empty string for localhost. Not used with sqlite3. 'PORT': '', # Set to empty string for default. Not used with sqlite3. } } when I try runserver I get an error (djangoenv) ubuntu@ip-172-31-16-133:~/webapps/kenyabuzz$ python manage.py runserver Performing system checks... System check identified some issues: WARNINGS: ?: (1_6.W001) Some project unittests may not execute as expected. HINT: Django 1.6 introduced a new default test runner. It looks like this project was generated using Django 1.5 or earlier. You should ensure your tests are all running & behaving as expected. See https://docs.djangoproject.com/en/dev/releases/1.6/#new-test-runner for more information. System check identified 1 issue … -
"python manage.py evolve --hint --execute" command failed with "Unicode equal comparison failed to convert both arguments to Unicode"
Installing review board 2.0.20 from bitnami stack installer. Facing issue while performing "python manage.py evolve --hint --execute command. error : C:\reviewboard\apps\reviewboard\Lib\site-packages\ReviewBoard-2.0.20-py2.7.egg>python reviewboard\ma nage.py evolve --hint --execute WARNING:py.warnings:C:\reviewboard\apps\django\django\db\models\loading.py:183: UnicodeWarning: Unic ode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequa l if app_label == app_name.split('.')[-1]: Traceback (most recent call last): File "reviewboard\manage.py", line 187, in main(settings, in_subprocess) File "reviewboard\manage.py", line 149, in main execute_from_command_line(sys.argv) File "C:\reviewboard\apps\django\django\core\management__init__.py", line 399, in execute_from_co mmand_line utility.execute() File "C:\reviewboard\apps\django\django\core\management__init__.py", line 392, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\reviewboard\apps\django\django\core\management\base.py", line 249, in run_from_argv stderr.write('%s: %s' % (e.class.name, e)) UnicodeDecodeError: 'ascii' codec can't decode byte 0x96 in position 15: ordinal not in range(128) Environment : windows 7, 64bit/32bit (tried both). Bitnami installer is installing python 2.7 as part of the review board installation. The same installation step worked in windows 8 and windows 10 32/64 bit machines. Any help is deeply appreciated in resolving this issue. Tried putting data.decode("utf-8") in the comparison line where failed in the error message. Any help would be deeply appreciated in resolving this issue. Thanks in advance. -kaushik -
django templates string formatting
I want to pad and cut too long chars. So I used stringformat. In this example, I want my string to be 10 characters long, with padding if necessary. I have this code in my django template {{ "my too long or too short string"|stringformat:":10" }} But django outputs nothing. -
Simplifying this django query - Have django obtain instances based on values
So I currently have this django query. The first two statements are needed in order to obtain the 3rd statement. My question is if there is a way to only use the 3rd statements without using the first two statements. #patient_name and quest are two strings patientobj = modelPatient.objects.get(patient_name=patient_name) questobj = modelInterviewQuestion.objects.get(question=quest) answer = modelInterviewAnswer.objects.get(patients=patientobj, questions=questobj) I know I could do something like this answer = modelInterviewAnswer.objects.get(patients= modelPatient.objects.get(patient_name=patient_name), questions= modelInterviewQuestion.objects.get(question=quest)) but I was wondering if there is anything simpler ? -
Django source code won't update on server
I have a Django website running and any updates I make to the source code won't update. (Reason I'm changing the file is because one line of code is generating an error. What's weird is I commented out this line of code that causes the error, but the code still runs and thus still causes the error. In the django.log it shows that line causing the error still, but it also shows it commented out now. So the error log shows my new source code, but the application itself isn't executing the new code) I am very new to Django, so I don't really know what's going on here (not my website, I got thrown on this project for work.) Researching around for this, I have already tried to restart apache: $ sudo apachectl restart $ sudo service apache2 restart and I've also tried to touch the wsgi.py file: $ touch wsgi.py and I have even deleted the .pyc file. Nothing has worked and the old line of code is still executing, even though the logs show it commented out. Not sure where else to check or what else I'm missing. -
Unable to render ForeignKey fields in my template
I'm making a comment system for my django app and i've been told it's best to make a seperate model for comment-voting. So i've done that and here's my models.py: class Comment(models.Model): user = models.ForeignKey(User, default=1) destination = models.CharField(default='1', max_length=12, blank=True) author = models.CharField(max_length=120, blank=True) comment_id = models.IntegerField(default=1) parent_id = models.IntegerField(default=0) comment_text = models.TextField(max_length=350, blank=True) timestamp = models.DateTimeField(default=timezone.now, blank=True) def __str__(self): return self.comment_text class CommentScore(models.Model): user = models.ForeignKey(User, default=1) comment = models.ForeignKey(Comment, related_name='score') upvotes = models.IntegerField(default=0) downvotes = models.IntegerField(default=0) def __str__(self): return str(self.comment) Here's my views.py where the comments are created: def article(request, category, id): name = resolve(request.path).kwargs['category'] for a, b in CATEGORY_CHOICES: if b == name: name = a instance = get_object_or_404(Post, id=id, category=name) allauth_login = LoginForm(request.POST or None) allauth_signup = SignupForm(request.POST or None) #comments comment = CommentForm(request.POST or None) ajax_comment = request.POST.get('text') comment_length = len(str(ajax_comment)) comment_list = Comment.objects.filter(destination=id) score = CommentScore.objects.filter(comment=comment_list) if request.is_ajax(): if comment.is_valid(): comment = Comment.objects.create(comment_text=ajax_comment, author=str(request.user), destination=id) print(comment) comment.save() score = CommentScore.objects.create(comment=comment) score.save() username = str(request.user) return JsonResponse({'text': ajax_comment, 'text_length': comment_length, 'username': username}) else: print(comment.errors) context = { 'score': score, 'comment_list': comment_list, 'comment': comment, 'instance': instance, 'allauth_login': allauth_login, 'allauth_signup': allauth_signup } return render(request, 'article.html', context) So the comment works fine, but as you can see …