1

Environment: Python 3.6 Django 2.0 Django REST 3

I am connecting a django project with an android application and everything has worked correctly but I have a problem that anyone who accesses the url of the requests in android can see that data. I want to block that render or template and only return the json data that I need for my android application.

myapp/View.py

class restContact(APIView):
    def get(self, request):
      allcontact = Contact.objects.all()
      serializer = ContactSerializer(allcontact, many=True)
      return Response({"state":"1","control":serializer.data},status=status.HTTP_400_BAD_REQUEST)

myapp/Url.py

from django.contrib import admin
from django.conf.urls import url,include
from apps.my-app.views import restContact


urlpatterns = [
    url(r'^contacts/$', restContact.as_view()),
]

proyect/url.py

from django.conf.urls import url
from django.urls import include
from django.contrib import admin

urlpatterns = [

    url(r'',include('apps.index.urls')),
    url(r'^admin/', admin.site.urls),
    url(r'^index/',include('apps.index.urls')),
    url(r'^movil/', include('apps.myapp.urls')),
]

I just need to return jsonObject and in the browser to write the url www.mypage.com/movil/contacts can not see all the data in the get.

creimers
  • 4,975
  • 4
  • 33
  • 55
J.leo
  • 81
  • 9
  • if you include `?format=json` in your URL, you should get the the raw JSON you want in your browser. – N. Ivanov Mar 13 '18 at 16:01
  • See http://www.django-rest-framework.org/api-guide/serializers/#additional-keyword-arguments more specifically you need to use on your seliazer extra_kwargs = {'url': {'write_only': True}}. I had the same issue on one of my projects and this resolved it for me. – Thanos Mar 13 '18 at 16:08
  • @Thanos Thanks, now you can not see the data but I still see django Rest's skin, Any solution for that? (my specialty is android does not django) – J.leo Mar 13 '18 at 18:07
  • @J.leo you are welcome. I am sorry I don't really understand what you mean by skin and what you are trying to remove. Can you provide me with a bit information so I can try to help you? – Thanos Mar 13 '18 at 22:17
  • @Thanos thanks for your help, what I want to remove is the template that is generated by entering the url of my api rest in the browser, that when you enter that url return the json object but the template in my browser is blank or redirect other page . (django rest whenever you return Response generates a buffer in the default browser). – J.leo Mar 14 '18 at 04:12
  • @J.leo To me it is not clear what exactly you are trying to do so maybe I am wrong. I think you are looking for this: https://stackoverflow.com/questions/523356/python-django-page-redirect Also you might be looking for this (https://docs.djangoproject.com/en/2.0/topics/http/shortcuts/) by modifying the return url. If this is the case you need to remove the previous solution that I provided you. Experiment a bit and I am sure you will get the output that you desire. Let me know if this is what you are looking for. – Thanos Mar 14 '18 at 10:04

0 Answers0