Author- Tushar Sonu Lambole
Installed packages
1) Django
2) PIL (for image related)
3) Django Rest framework (for Rest APIs)
4) pip
5) South (for database alterations)
1.
To view the table in sqlite database python
python manage.py shelldb
SELECT * FROM sqlite_master WHERE type='table';
DROP TABLE appname_modelname
.exit
2. Drops and re-creates the tables used by the models of this app.
python manage.py reset app_name
----------------------------------------------------------------------------------------------------
AR App
models.py
__author__ = 'tusharlambole'
from django.db import models
from django.contrib import admin
import os
class ProjectMaster(models.Model):
p_id = models.AutoField(primary_key=True)
p_name = models.CharField(max_length=150)
p_description = models.TextField()
p_date = models.DateTimeField()
def __unicode__(self):
return (self.p_name)
class Meta:
ordering = ('-p_date',)
class ProjectMasterAdmin(admin.ModelAdmin):
list_display = ('p_id','p_name','p_description','p_date')
admin.site.register(ProjectMaster, ProjectMasterAdmin)
class ClientMaster(models.Model):
c_id = models.AutoField(primary_key=True)
p_fk = models.ForeignKey(ProjectMaster)
c_name = models.CharField(max_length=150)
def image_path(instance, filename):
return os.path.join('ImagesFiles', str(instance.c_name), str(instance.c_name)+'.png')
c_access_key = models.CharField(max_length=150)
c_secret_key = models.CharField(max_length=150)
c_is_active = models.BooleanField(default=False)
c_logo = models.ImageField(upload_to=image_path)
def __unicode__(self):
return (self.c_name)
class Meta:
ordering = ('c_id',)
class ClientMasterAdmin(admin.ModelAdmin):
list_display = ('c_id','c_name','c_is_active','c_logo')
admin.site.register(ClientMaster,ClientMasterAdmin)
----------------------------------------------------------------------------------------------------
serializers.py
from models import ProjectMaster,ClientMaster
from rest_framework import serializers
class ProjectMasterSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = ProjectMaster
fields = ('p_name','p_description','p_date')
class ClientMasterSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = ClientMaster
fields = ('c_id','c_name','c_is_active','p_fk','c_access_key','c_secret_key','c_logo')
----------------------------------------------------------------------------------------------------
views.py
__author__ = 'tusharlambole'
from models import ProjectMaster,ClientMaster
from serializers import ProjectMasterSerializer,ClientMasterSerializer
from rest_framework import viewsets
class ProjectMasterViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = ProjectMaster.objects.all()
serializer_class = ProjectMasterSerializer
class ClientMasterViewSet(viewsets.ModelViewSet):
queryset = ClientMaster.objects.all()
serializer_class = ClientMasterSerializer
----------------------------------------------------------------------------------------------------
settings.py
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'FirstSite',
'rest_framework',
'AR',
'south',
)
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAdminUser',),
'PAGINATE_BY': 10
}
SESSION_SERIALIZER = 'django.contrib.sessions.serializers.JSONSerializer'
----------------------------------------------------------------------------------------------------
urls.py
from django.conf.urls import patterns, include, url
from FirstSite.views import archieve,GroupViewSet,UserViewSet
from rest_framework import routers
from AR.views import ProjectMasterViewSet,ClientMasterViewSet
from django.contrib import admin
admin.autodiscover()
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
router.register(r'groups', GroupViewSet)
router.register(r'projectmaster', ProjectMasterViewSet)
router.register(r'clientmaster', ClientMasterViewSet)
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'TusharWebApp.views.home', name='home'),
url(r'^blog/', include('FirstSite.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
)
----------------------------------------------------------------------------------------------------
FirstSite App
templates/base.html
<html>
<style type="text/css">
body { color: #efd; background: #453; padding: 0 5em; margin: 0 } h1 { padding: 2em 1em; background: #675 }
h2 { color: #bf8; border-top: 1px dotted #fff; margin-top: 2em } p { margin: 1em 0 }
</style>
<body>
<h1>Tushar Sonu Lambole</h1>
{% block content %}
{% endblock %}
</body>
</html>
----------------------------------------------------------------------------------------------------
templates/archieve.html
{% extends "base.html" %}
{% block content %}
{% for post in posts %}
<h2>{{ post.title }}
</h2> <p>{{ post.timestamp|date:"l, F jS" }}</p>
<p>{{ post.body }}</p>
{% endfor %}
{% endblock %}
----------------------------------------------------------------------------------------------------
models.py
from django.db import models
from django.contrib import admin
# Create your models here.
class BlogPost(models.Model):
title = models.CharField(max_length=150)
body = models.TextField()
timestamp = models.DateTimeField()
class Meta:
ordering = ('-timestamp',)
class BlogPostAdmin(admin.ModelAdmin):
list_display = ('title','timestamp')
admin.site.register(BlogPost, BlogPostAdmin)
----------------------------------------------------------------------------------------------------
urls.py
from django.conf.urls import patterns, include, url
from FirstSite.views import archieve
urlpatterns = patterns('',url(r'^$',archieve),)
----------------------------------------------------------------------------------------------------
views.py
from django.shortcuts import render
from django.template import loader, Context
from django.http import HttpResponse
from FirstSite.models import BlogPost
def archieve(request):
posts = BlogPost.objects.all()
t = loader.get_template("archive.html")
c = Context({ 'posts': posts })
return HttpResponse(t.render(c))