V
V
V2016-07-12 22:43:19
Docker
V, 2016-07-12 22:43:19

How to implement on django rest api?

you need to make a simple rest api on django. The post request must contain the user's ip. I recently switched to django from rails, while at a loss (
What is already there:
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')

views.py
from django.contrib.auth.models import User
from rest_framework import viewsets
from tutorial.quickstart.serializers import UserSerializer


class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer

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

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

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
]

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAdminUser',),
    'PAGE_SIZE': 10
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman Kitaev, 2016-07-12
@deliro

Django Rest Framework or tastypie

K
Keofaste, 2016-07-12
@Keofaste

Alternatively use django.http.JsonResponse.

I
IvanOne, 2016-07-13
@IvanOne

Django Rest Framework there, as far as I remember, the same request as in django itself, you can easily get ip in a view or with a serializer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question