Coder Social home page Coder Social logo

adyg / django-heroku-s3 Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 48 KB

Django 1.10 project template with built-in Heroku, Vagrant, Amazon S3, Sass and Solr support.

Shell 2.83% Ruby 12.37% Python 43.04% CSS 38.98% JavaScript 0.12% HTML 2.66%
django django-heroku django-template-project

django-heroku-s3's Introduction

django-heroku-s3

Django 1.10 project template with built-in Heroku, Vagrant, Amazon S3, Sass and Solr support.

What you will end up with

  1. Django project skeleton
  2. Settings for running the project on Heroku, using waitress and S3 for static files.
  3. Custom bash scripts for pushing to Heroku, publishing assets to S3 and running the dev server.
  4. Vagrant box setup with basic development environment (postgresql, pip, heroku toolbelt, git, sass and solr support)

Prerequisites

  1. A Heroku account
  2. An Amazon AWS account
  3. Vagrant installed
  4. Ansible installed

Basic Usage

  • Start a new django project with (make sure to change projectname to the actual project name): $ django-admin startproject --template=https://github.com/Adyg/django-heroku-s3/archive/master.zip --extension sh,py,pp,yml --name Vagrantfile,Procfile projectname

  • Ensure all packages required by Ansible are present by running: $ ansible-galaxy install -r requirements.yml (from inside the projectname/vagrant dir)

  • Start up the vagrant box with (this might take a while): $ vagrant up (from inside the projectname/vagrant dir)

  • SSH into the vagrant box with $ vagrant ssh (from inside the projectname/vagrant dir). The project will be available under /vagrant_data. A postgresql database will be automatically created (the username/pass are the [projectname])

  • Use $ heroku auth:login to authenticate with Heroku

Amazon S3

  • Create an Amazon S3 bucket
  • After creating the bucket, under its Properties > Permissions section, update its CORS configuration to something along the lines of:
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>PUT</AllowedMethod>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>
  • Create a new IAM User (remember to download its access key and secret access key)
  • Go to the new IAM User's page and attach a new policy to allow access to S3. Something similar to (replace BUCKET_NAME with the actual name of your bucket):
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "s3:ListAllMyBuckets",
            "Resource": "arn:aws:s3:::*"
        },
        {
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::BUCKET_NAME",
                "arn:aws:s3:::BUCKET_NAME/*"
            ]
        }
    ]
}

Heroku

  • Create an app on Heroku (aim for its name to be the same as the Django project name, otherwise customization to the bash scripts will be needed)
  • Inside the vagrant box, go to the /vagrant_data dir and run $ heroku git:remote -a [projectname]
  • Update the project Heroku settings (can be run inside the vagrant box, under the /vagrant_data dir):
$ heroku config:set DJANGO_SETTINGS_MODULE=projectname.settings.heroku
$ heroku config:set AWS_STORAGE_BUCKET_NAME=[S3 bucket name]
$ heroku config:set AWS_ACCESS_KEY_ID=[S3 access key]
$ heroku config:set AWS_SECRET_ACCESS_KEY=[S3 secret access key]
$ heroku config:set SECRET_KEY=random_string
$ heroku config:set DISABLE_COLLECTSTATIC=1
  • Inside the vagrant box, run the $ ./deploy_heroku.sh script to push the project to Heroku

Note: the static assets will not be pushed to Heroku (the included .slugignore file prevents it). Instead, they should be published to S3 via the included $ ./publish_assets.sh (from inside the vagrant box). The reason is to reduce the Heroku slug size as much as possible.

Dev Server

The Django development server can be started inside the Vagrant box by using the $ /vagrant_data/run_dev_server.sh script. The development server will be available on the host machine at http://localhost:9198 (you can change the port in the Vagrantfile)

Sass

CSS files can be built with

$ cd /vagrant_data
$ ./compile_sass.sh

django-heroku-s3's People

Contributors

adyg avatar

Watchers

 avatar  avatar

django-heroku-s3's Issues

heroku app not showing images how to render images from s3

i uploaded my site on heroku but its not showing the images. so i want to use s3 to store my media file but i dont know what is the procedure to store media files on s3 and how heroku bring that images on my site. i already having

  • AWS_STORAGE_BUCKET_NAME = 'darkmachine'
    AWS_ACCESS_KEY_ID = 'AKIAJLAXXXXXX'
    AWS_SECRET_ACCESS_KEY = '64Ezs4wRpG7XXXXXXXXXXXXXX'

i set this in my heroku Config Variables

Here is my settins.py file

`
from django.conf import settings

if not settings.DEBUG:
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

SECRET_KEY = 'csqwlmc8s55o($rt6ozh7u+ui9zb-et00w$d90j8$^!nvj41_r'


DEBUG = False

ADMINS = (
    ("rahul", "[email protected]"),

)

ALLOWED_HOSTS = ["flyingstore.herokuapp.com"]
# purchasing domain name http://name.com

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'yourpassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

''' 
If using gmail, you will need to
unlock Captcha to enable Django 
to  send for you:
https://accounts.google.com/displayunlockcaptcha
'''

# Application definition

INSTALLED_APPS = (
    # django app
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # third party apps
    'crispy_forms',
    'django_filters',
    'registration',
    # my apps
    'carts',
    'newsletter',
    'orders',
    'products',
    'storages',
    'boto',
)
MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'ecommerce2.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, "templates")],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'ecommerce2.wsgi.application'



DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'flying_store',
        'USER': "postgres",
        'PASSWORD': 'XXX6612x',
        'HOST': '',
        'PORT': ''
    }
}
import dj_database_url

db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)



LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

# STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'


STATIC_URL = '/static/'
MEDIA_URL = '/media/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static_in_pro", "our_static"),
   
)

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_in_env", "static_root")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_in_env", "media_root")



STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',

)

# Crispy FORM TAGs SETTINGS
CRISPY_TEMPLATE_PACK = 'bootstrap3'

# DJANGO REGISTRATION REDUX SETTINGS
ACCOUNT_ACTIVATION_DAYS = 7
REGISTRATION_AUTO_LOGIN = True
SITE_ID = 1
LOGIN_REDIRECT_URL = '/'

BRAINTREE_PUBLIC = "2f8dqqhqs7wz95t8"
BRAINTREE_PRIVATE = "9d2f9ddfaefd81d6af09d537017d86c4"
BRAINTREE_MERCHANT_ID = "68nvwyj4qsz7rnzc"
BRAINTREE_ENVIRONEMNT = "Sandbox"
`
here is my bucket PERMSSION- CORS configration

<?xml version="1.0" encoding="UTF-8"?> <CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <CORSRule> <AllowedOrigin>https://flyingstore.herokuapp.com/</AllowedOrigin> <AllowedMethod>GET</AllowedMethod> <AllowedMethod>POST</AllowedMethod> <AllowedMethod>PUT</AllowedMethod> <AllowedHeader>*</AllowedHeader> </CORSRule> </CORSConfiguration>

now tell me what i am messing what i have to add in my code. i tried lot but not getting the solution please help me .

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.