0

I am trying to implement Push Notifications in my Python Django App.

Project Structure:

.
├── db.sqlite3
├── manage.py
├── example
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py
├── README.md
├── requirements.txt
├── static
│   ├── js
│        ├── sw.js
│        ├── push.js
└── templates
    └── landing
         ├── homepage.html

Now,

Homepage.html file includes a script

<script src="{% static 'js/push.js' %}"></script>

Push.js includes:

navigator.serviceWorker.register('../static/js/sw.js').then(function(serviceWorkerRegistration) {

And let's say the this webApp is hosted on www.example.com.

Now, I have two queries:

1) I can access the file at www.example.com/static/js/sw.js, But not on www.example.com/sw.js. If I move sw.js file at the root directory, and try www.example.com/sw.js then I get the following Error:

Using the URLconf defined in example.urls, Django tried these URL patterns, in this order: (and followed by a llist of urls mentioned in urls.py)

Also, If i run 'python manage.py collectstatic' command then the file sw.js automatically gets moved to static/js/sw.js only. So, How can I make www.example.com/sw.js work ?

2) Can it be possible to host sw.js on Google Storage Bucket/AWS Bucket/somewhere else and fetch this file from there ? ( Or Can absolute path of the file hosted on remote server be used )

Tried to google this query but couldn't find the satisfactory answer for these issues.

Any suggestions would be useful.

Thanks,

Naveen
  • 677
  • 1
  • 11
  • 27
  • What is the problem for you if you are accessing by `www.example.com/static/js/sw.js` ? and why you want to access `www.example.com/sw.js` – kartheek Sep 07 '16 at 07:54
  • No problem as of now. But If sw.js is in root directory, then it might have some more control over the project. – Naveen Sep 07 '16 at 07:57
  • Actually I am more concerned about the 2nd question. Any suggestion for that ? – Naveen Sep 07 '16 at 07:58
  • As per Django project structure media files like Script files(.js), CSS, Images in static directory for several reasons. – kartheek Sep 07 '16 at 08:03
  • Ans for Q.2: Yes you can put your media/static files in amazon s3 bucket and use/access in your application, [refer this article](https://www.caktusgroup.com/blog/2014/11/10/Using-Amazon-S3-to-store-your-Django-sites-static-and-media-files/). – kartheek Sep 07 '16 at 08:07
  • Possible duplicate of [Implementing push notification using chrome in Django](http://stackoverflow.com/questions/34389485/implementing-push-notification-using-chrome-in-django) – MariusSiuram Oct 23 '16 at 08:01

0 Answers0