Skip to content

snamstorm/Django_Setup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 

Repository files navigation

Setting up a Django Project in PyCharm

  • In the terminal, mkvirtualenv PROJECT_NAME
  • In the virtualenv, pip install django
  • In the virtualenv, pip install psycopg2
  • In the virtualenv, django-admin.py startproject PROJECT_NAME
  • If the DB isn't running, in the virtualenv, run postgres -D /usr/local/var/postgres
  • In the virtualenv, psql postgres
  • In postgres, open create database PROJECT_NAME;
  • In PyCharm, create the file local_settings.py
  • In local_settings.py paste
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'PROJECT_NAME',
    }
}
  • In settings.py, under the DATABASES section paster
try:
    from local_settings import *
except ImportError:
    pass
  • In Pycharm, click on PyCharm -> Preferences -> Project Interpreter -> Gear Icon -> Add Local. Go to your home directory -> .virtualenvs -> PROJECT_NAME -> bin -> python

Setting up Django App

  • In virtualenv, cd PROJECT_NAME (same level as manage.py
  • In virtualenv, python manage.py startapp APP_NAME
  • In PyCharm project, open settings.py and in INSTALLED_APPS add 'APP_NAME'

Creating Data Model

  • In models.py, under from django.db import models paste
class TABLE_NAME(models.Model):
    variable1 = models.CharField(max_length=120)
    variable2 = models.CharField(max_length=40, null=True)

    def __unicode__(self):
        return u"{}".format(self.variable1)

Setting up Django Admin

  • When using the built-in Django admin panel, this step always goes together with creating a data model
  • In PyCharm, open admin.py and paste
from django.contrib import admin
from your_app.models import TABLE_NAME

class TABLE_NAMEAdmin(admin.ModelAdmin):
    fields = ['variable1','variable2']

admin.site.register(TABLE_NAME, TABLE_NAMEAdmin)

Create Tables in Database

  • In virtualenv, python manage.py makemigrations
  • In virtualenv, python manage.py migrate

###Create SuperUser

  • In virtualenv, python manage.py createsuperuser
  • In Username: , enter an admin_name
  • In Email address: , enter admin_email
  • In Password: , enter a password
  • In PyCharm, check settings.py under INSTALLED_APPS and make sure you see 'django.contrib.admin',

About

Setting up Django in PyCharm

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published