Showing posts with label django-Rest Project-1(Tutorial). Show all posts
Showing posts with label django-Rest Project-1(Tutorial). Show all posts

10. let see what is api root from output of rest framework and find out inital review

ok in the output

u can see api root which is acutally very important when we talk about REST Framework




ok it is little bit confusing when we see the output..... ie.


 
o/p (when it is default page before login )
====
 
 
 
GET /
HTTP 403 Forbidden
Content-Type: application/json
Vary: Accept
Allow: GET, HEAD, OPTIONS

{
    "detail": "Authentication credentials were not provided."
}
 
 
 
o/p(after login the status of out put)
 
====
 
 
GET /
HTTP 200 OK
Content-Type: application/json
Vary: Accept
Allow: GET, HEAD, OPTIONS

{
    "users": "http://127.0.0.1:8000/users/",
    "groups": "http://127.0.0.1:8000/groups/"
}
 
 
 
 
 
 
 
so let us see one by one heading where we can identify from the out put
 
 
ie., 
 
 
- GET 
- HTTP
- Content-Type
- Vary
- Allow

so to understand this we need to go some more deep about REST Framework....


to make some backup study...... to get what is going here...... right 



so that we will see in the next post about Rest Framework .....






9. let we see and understand about the output of rest in django








let us see what we can learn from the output


ok on the top

the django rest framework is given ie., 3.1.3




ok i think you can see the right side there is login....
so when we click on that we are getting the output like






next there is api root and we will see that in next post.....







8. create serializers.py in app ie., quickstart ,update (view.py,url.py,setting.py) and check the rest api after runserver


ok write the following in terminal....



(env)erp@erp-Inspiron-N5110:~/tutorial$ python manage.py syncdb
/home/erp/tutorial/env/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py:24: RemovedInDjango19Warning: The syncdb command will be removed in Django 1.9
  warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDjango19Warning)

Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  No migrations to apply.

You have installed Django's auth system, and don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'erp'): erp
Email address: erp@gmail.com
Password:
Password (again):
Superuser created successfully.
(env)erp@erp-Inspiron-N5110:~/tutorial$ python manage.py runserver


 


o/p








why we are seeing this is becuase we will write serializer code for admin only ie.,

user and group





create in app ie., quickstart with the file name 


serializers.py

from django.contrib.auth.models import User, Group
from rest_framework import serializers

class UserSerializer(serializers.HyperlinkedModelSerializer):
         class Meta:
                  model = User
                  fields = ('url', 'username', 'email', 'groups')
class GroupSerializer(serializers.HyperlinkedModelSerializer):
         class Meta:
                  model = Group
                  fields = ('url', 'name')




View.py
-----------

from django.shortcuts import render
from django.contrib.auth.models import User,Group
from rest_framework import viewsets
from quickstart.serializers import UserSerializer, GroupSerializer


class UserViewSet(viewsets.ModelViewSet):
      queryset = User.objects.all()
      serializer_class = UserSerializer


class GroupViewSet(viewsets.ModelViewSet):
      queryset = Group.objects.all()
      serializer_class = GroupSerializer

url.py
=====
from django.conf.urls import patterns, include, url
from rest_framework import routers
from quickstart import views

router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups',views.GroupViewSet)


urlpatterns = patterns('',
    # Examples:
     url(r'^', include(router.urls)),
     url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),


)
 
 
 
setting.py
---------
 
 
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'quickstart',
    'rest_framework',
)

REST_FRAMEWORK = {
       'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAdminUser',),
       'PAGINATE_BY':10
}
 
 
 
 
 
in terminal
 
====
 
(env)erp@erp-Inspiron-N5110:~/tutorial$ python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
June 12, 2015 - 09:53:28
Django version 1.8.2, using settings 'tutorial.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.




 




7(b). Xml vs Json














let us consider the example so that we will get the idea what is xml and json

these both are content type


What is JSON?

  • JSON stands for JavaScript Object Notation
  • JSON is a lightweight data-interchange format
  • JSON is language independent *
  • JSON is "self-describing" and easy to understand







  • The fundamental difference between XML and JSON is that XML is a meta-language and JSON is a markup language. 
  • That is, XML syntax is designed specifically to have no inherent semantics. 
  • Particular element names don't mean anything until a particular processing application processes them in a particular way. 
  • By contrast, JSON syntax has specific semantics built in: stuff between {} is an object, stuff between [] is an array, etc.
  • A JSON parser, therefore, knows exactly what every JSON document means. 
  • An XML parser only knows how to separate markup from data. 

7(a). let us talk Serializers before doing with django








Serializers

Expanding the usefulness of the serializers is something that we would like to address. However, it's not a trivial problem, and it will take some serious design work.
Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON, XML or other content types.

Serializers also provide deserialization, allowing parsed data to be converted
back into complex types, after first validating the incoming data.

The serializers in REST framework work very similarly to Django's Form and ModelForm classes.

We provide a Serializer class which gives you a powerful, generic way to control the output of your responses, as well as a ModelSerializer class which provides a useful shortcut for creating serializers that deal with model instances and querysets.



 Higligthed Topics from above  we need to know ie.,

- json vs xml
- django form
- model form




 

6. Let us create app and do syncdb

(env)erp@erp-Inspiron-N5110:~/tutorial$ python manage.py startapp quickstart
(env)erp@erp-Inspiron-N5110:~/tutorial$python manage.py syncdb

5. migrate and run the django project

(env)erp@erp-Inspiron-N5110:~/tutorial$ python manage.py mirgrate
Unknown command: 'mirgrate'
Type 'manage.py help' for usage.
(env)erp@erp-Inspiron-N5110:~/tutorial$ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying sessions.0001_initial... OK
(env)erp@erp-Inspiron-N5110:~/tutorial$ python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
June 11, 2015 - 16:38:20
Django version 1.8.2, using settings 'tutorial.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.



and u will get the output



4. Let us check what are there in the project folder




so in the current project we have


1. env folder ie., environement where all installation and dependence files will be there
2. tutorial folder ie., is acutall django project which we understand eariler only

3. manage.py ie., this is file where we use for so many activities in django project.

 

3. Let us install dependencies with pip

what is pip?
 
PIP stands for Pip Installs Python or PIP Installs Packages
=====================================


ok let us start dependencies and basic installation


(env)erp@erp-Inspiron-N5110:~/tutorial$ pip install django
Downloading/unpacking django
Downloading Django-1.8.2-py2.py3-none-any.whl (6.2MB): 6.2MB downloaded
Installing collected packages: django
Successfully installed django
Cleaning up...

(env)erp@erp-Inspiron-N5110:~/tutorial$ pip install djangorestframework
Downloading/unpacking djangorestframework
  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):   0%  4.1k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):   1%  8.2k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):   2%  12kB  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  20%  102k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  21%  106k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  22%  110k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  23%  114k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  24%  118k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  25%  122k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  25%  126k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  26%  131k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  27%  135k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  28%  139k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  29%  143k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  30%  147k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  30%  151k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  31%  155k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  32%  159k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  33%  163k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  34%  167k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  35%  172k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  35%  176k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  36%  180k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  37%  184k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  38%  188k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  39%  192k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  40%  196k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  40%  200k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  41%  204k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  42%  208k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  43%  212k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  44%  217k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  45%  221k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  45%  225k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  46%  229k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  47%  233k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  48%  237k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  49%  241k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  50%  245k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  50%  249k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  51%  253k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  52%  258k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  53%  262k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  54%  266k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  55%  270k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  55%  274k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  56%  278k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  57%  282k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  58%  286k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  59%  290k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  60%  294k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  60%  299k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  61%  303k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  62%  307k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  63%  311k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  64%  315k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  65%  319k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  65%  323k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  66%  327k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  67%  331k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  68%  335k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  69%  339k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  70%  344k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  70%  348k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  71%  352k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  72%  356k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  73%  360k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  74%  364k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  75%  368k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  75%  372k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  76%  376k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  77%  380k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  78%  385k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  79%  389k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  80%  393k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  80%  397k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  81%  401k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  82%  405k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  83%  409k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  84%  413k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  85%  417k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  85%  421k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  86%  425k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  87%  430k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  88%  434k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  89%  438k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  90%  442k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  90%  446k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  91%  450k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  92%  454k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  93%  458k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  94%  462k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  95%  466k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  95%  471k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  96%  475k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  97%  479k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  98%  483k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):  99%  487k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB): 100%  491k  Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB):             Downloading djangorestframework-3.1.3-py2.py3-none-any.whl (491kB): 491kB downloaded
Installing collected packages: djangorestframework
Successfully installed djangorestframework
Cleaning up...

2. use virutal env and find what is the difference

erp@erp-Inspiron-N5110:~/tutorial$ source env/bin/activate
(env)erp@erp-Inspiron-N5110:~/tutorial$


so when i activate the virutal environement ie., env with the commmand ie., source env/bin/activate(ie., source to active is env/bin/activate )


and we can see the env which i highlight with the red color.....



so it means that it is in virtual environement.  fine...





1. Install virutal machine ,Create django project and create virutal environement

hai,

this is deepak ,let us try what is django with REST framework which is currently very popular webservice in the market.



so let first install virtualenv with the following command in the terminal ie.,



$sudo apt-get install python-virtualenv

Now create the project ie., tutorial


$ django-admin startproject tutorial
$ cd tutorial
$ virtualenv env

New python executable in env/bin/python
Installing setuptools, pip...done.