sudo easy_install pip
sudo pip install setuptools --no-use-wheel --upgrade
sudo pip install virtualenv
sudo pip install virtualenvwrapper
cd $HOME
mkdir .virtualenvs
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bash_profile
. /usr/local/bin/virtualenvwrapper.sh
mkvirtualenv <your_env_name>
pip install django
django-admin.py startproject --template=https://github.com/zincsoda/heroku-django-template/archive/master.zip --name=Procfile meetups
pip install -r requirements.txt
./manage.py syncdb
./manage.py runserver 0.0.0.0:5005
Open meetups/settings.py and add 'meetups' to INSTALLED_APPS
Create meetups/views.py and add the following lines:
from django.http import HttpResponse
def test(request):
return HttpResponse("Hello WWCode")
Open urls.py.
Import our newly created view module
import meetups2.views
Add the line to the urls tuple:
url(r'^test$','meetups2.views.test'),
In views.py, add a python dictionary of users and return a json response instead. Change the file as follows:
from django.http import HttpResponse
import json
def test(request):
my_dict = {'users': ['bob','mary','kate']}
json_data = json.dumps(my_dict)
return HttpResponse(my_dict)
return HttpResponse(json_data, content_type="application/json")
Create a file called meetups/models.py with the following contents:
from django.db import models
class Image(models.Model):
name = models.CharField(max_length=200, blank=True, null=True)
image_url = models.CharField(max_length=1000, blank=True, null=True)
def __unicode__(self):
return self.name
class Meetup(models.Model):
name = models.CharField(max_length=200, blank=True, null=True)
description = models.TextField(blank=True, null=True)
meeting_time = models.CharField(max_length=200, blank=True, null=True)
location = models.CharField(max_length=200, blank=True, null=True)
postcode = models.CharField(max_length=12, blank=True, null=True)
contact = models.CharField(max_length=200, blank=True, null=True)
image = models.ForeignKey(Image, blank=True, null=True, on_delete=models.SET_NULL)
website = models.CharField(max_length=500, blank=True, null=True)
def __unicode__(self):
return self.name
Run:
./manage.py migrate
Open up db.sqlite3 and show the tables we have created.
Create a file meetups/admin.py
from django.contrib import admin
from models import *
class MeetupAdmin(admin.ModelAdmin):
list_display = ('name','description', 'contact', 'location')
admin.site.register(Meetup, MeetupAdmin)
class ImageAdmin(admin.ModelAdmin):
list_display = ('name','image_url',)
admin.site.register(Image, ImageAdmin)
Install the django rest framework:
pip install djangorestframework
Add 'rest_framework' to INSTALLED_APPS
Create a file called meetsup/serialzers.py and add the following content:
from rest_framework import serializers
from meetups2.models import *
class ImageSerializer(serializers.ModelSerializer):
class Meta:
model = Image
fields = ('id','image_url')
class MeetupSerializer(serializers.ModelSerializer):
image_url = serializers.CharField(source='image.image_url', read_only=True)
class Meta:
model = Meetup
fields = ('id','name','description','meeting_time','location','postcode','contact','image_url')
#15. Create Mixin classes for Meetups
In views.py, add the following headers
from meetups2.models import Meetup
from meetups2.serializers import MeetupSerializer
from rest_framework import generics
Then add the following classes:
class MeetupList(generics.ListCreateAPIView):
queryset = Meetup.objects.all()
serializer_class = MeetupSerializer
class MeetupDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Meetup.objects.all()
serializer_class = MeetupSerializer
Open urls.py, add the follwing import to
from rest_framework.urlpatterns import format_suffix_patterns
Add this code to end of urls.py
urlpatterns = format_suffix_patterns(urlpatterns)
Then add the following lines to the url dispatcher tuple:
url(r'^meetups$', views.MeetupList.as_view(), name='meetup-list'),
url(r'^meetups/(?P<pk>[0-9]+)$', views.MeetupDetail.as_view(), name='meetup-detail'),
/meetups will now show you the lovely Django REST Frameworks UI for interacting with your models through REST
#18. Push to Heroku
Capture all new python dependencies in the requirements file:
pip freeze > requirements.txt
You may need to first install heroku toolbelt. Then type the following command:
heroku create
This will create a heroku app with a generated name. It will also set up your git repository with a new remote for heroku so that you can now just push your repo to heroku using the following command:
git push heroku master
Remember that you need to initialize your new heroku database:
heroku run python manage.py syncdb
Navigate to the URL and it should be working.