Coder Social home page Coder Social logo

django-blog-rep's Introduction

Django Blog

SETUP

  1. Install Gunicorn
  • pip3 install 'django<4' gunicorn
  1. Then two database libraries are needed
  • pip3 install dj_database_url==0.5.0 psycopg2
  1. Then install library needed to run Cloudinary
  • pip3 install dj3-cloudinary-storage
  1. Create requirements.txt file
  • pip3 freeze --local > requirements.txt
  1. Now we can create a new Django project
  • django-admin startproject blog .
  1. Then we start a new app
  • python3 manage.py startapp blogapp
  1. Now we need to add it to installed app in the settings.py file

  2. We also need to migrate the changes. Whenever add a new app it is automatically added to migrate and we need to give the command

  • python3 manage.py migrate
  1. Now we can do the runserver command. We need to add the Django server that comes up into our allowed hosts in settings.py

  2. Now, we want to prepare it for deployment to Heroku. We’re into the third step of our “new project checklist” which is setting the project up to use Cloudinary and an external database.

To do this we need to create the app on Heroku, provision a database, configure some environment variables, and modify the settings of the project to get all this working.

  1. So first we create a Heroku app

  2. Then we create a ElephantSQL database

  3. Now we create an env.py file and import os at the top. After this we add:

  • os.environ["DATABASE_URL"]="" (where url is db url from ElephantSQL)

  • os.environ["SECRET_KEY"]="my_super^secret@key" (secret key can be whatever, but maybe take the one Django generated)

  1. Now we need to modify the settings.py file first we import env.py

import os import dj_database_url if os.path.isfile('env.py'): import env

Then:

  • SECRET_KEY = os.environ.get('SECRET_KEY')
  1. Now that is taken care of, we need to hook up the database. We are going to use the dj_database_url import for this, so scroll down in your settings file to the database section.

Comment out the original DATABASES variable and add the code below, as shown

DATABASES = { 'default': dj_database_url.parse(os.environ.get("DATABASE_URL")) }

The code that has been commented out connects your Django application to the created db.sqlite3 database within your repo. However, as we know, that database is not suitable for production. This line of code separates the database URL stored by your env.py file into the relevant name and password etc.

  1. We should be able to connect to the ElephantSQL database now. First we need to migrate it.
  • python manage.py migrate

15.Time to push our changes to Github

  1. Now comes the tricky part. Setting up Heroku

We start off by adding the database and secret key as config vars in Heroku: Add two config vars:

  • DATABASE_URL, and for the value, copy in your database URL from ElephantSQL, no need to add quotation marks.

  • SECRET_KEY containing your secret key

We also add

  • PORT : 8000
  1. Then we add the API environment key from Cloudinary into the env.py
  • os.environ['CLOUDINARY_URL'] = 'secret url' (and remove cloudinary_url= from the code)
  1. Add the CLOUDINARY_URL to Heroku. The one without cloudinary_url

  2. Also add DISABLE_COLLECTSTATIC and 1 to the config vars

  3. Then under installed apps in settings.py add right above django.contrib.staticfiles add:

  • cloudinary_storage

and then under staticfiles add:

  • 'cloudinary',
  1. Then near the end of our settings.py file we can add these few lines. First of all, "STATICFILES_STORAGE" and in here we can tell it to use, "Cloudinary_storage.storage.StaticHashedCloudinaryStorage", so this is coming from the library that we installed above, and put into our installed apps section.

  2. THen right under I add

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'staticfiles')] STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

MEDIA_URL = '/media/' DEFAULT_FILE_STORAGE = 'cloudinary_storage.storage.MediaCloudinaryStorage'

  1. Then further up under BASE_DIR add:

TEMPLATES_DIR = os.path.join(BASE_DIR, 'templates')

  1. Then still in settings.py under TEMPLATES change the value of 'DIRS' to

[TEMPLATES_DIR]

  1. then under allowed hosts, write

'heroku_app_name.herokuapp.com', 'localhost'

  1. add folders media, static, templates at root

  2. Finally add a Procfile and within write:

web: gunicorn blog.wsgi

django-blog-rep's People

Contributors

christiangoran avatar

Watchers

 avatar

django-blog-rep's Issues

USER STORY: View Likes

As a site user / Admine I can view the number of likes on each photo so that I can see which is the most popular or viral

USER STORY: View Post Lists

As a site user I can view a list of posts so that I can select one to read

As a Site User I can view a list of posts so that I can select one to read

USER STORY: Open a Post

As a site user I can click on a post so that I can read the full text

As a Site User I can click on a post so that I can read the full text

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.