2

I have my Django application deployed on Heroku in a free dyno Currently, I am sending some GET and POST requests from JQuery-1.12.4 version

In my local development version of my application, the requests to my REST endpoints services it’s works Here an example of the fetch of posts of an user, consuming from ajax to endpoint rest service

enter image description here

This is my localhost Django server

I deploy on Heroku free dyno this same user interface, but the behavior is the following:

enter image description here

The server throw an error 500, although was happen some inusual or strange Previously, when I create an user for first time in my Heroku deployment to this user, the create and fetch post functionality it’s works O.K. like in my local environment development.

Heroku accept GET and POST request from JQuery AJAX ONLY to this user, such as follow in this picture:

enter image description here

I Unknown the reason which Heroku like production/testing server deployment does not accept or accept limited AJAX requests. Is possible (I think so, and will be too logic) that this behavior to be due to I am using free dyno heroku server?

Why only accept ajax request to just an user and other users like the images show above, does not works the ajax requests?

In this question talk about the topic, but I don’t have clear this subject

What other option suggest to me in relation to deployment of my project?

AWS EC2? may be?

The Heroku free dyno, don't let me look the logs due to in this free dyno, does not saved files ..

I appreciate some orientation about it.

Best Regards

UPDATE

I can look the logs in my heroku deployment, according to this answer

By the moment, the logs in the GET and POST operation only tell me about of status 500

This is the logs when I perform the GET request

2017-09-01T12:34:31.053127+00:00 heroku[router]: at=info method=GET path="/api/post/?q=" host=hostayni.herokuapp.com request_id=acd319f7-d47d-4e6c-833f-a0ef34163c51 fwd="190.240.77.93" dyno=web.1 connect=1ms service=615ms status=500 bytes=253 protocol=http

And this is the log when I perform the POST request

2017-09-01T12:37:56.512868+00:00 heroku[router]: at=info method=POST path="/api/post/create/" host=hostayni.herokuapp.com request_id=6d51cd89-431e-4b6e-80fe-55a505433fcc fwd="190.240.77.93" dyno=web.1 connect=1ms service=150ms status=500 bytes=253 protocol=http

In this moment I don't know how to proceed in relation to status=500 from my JQuery ajax requests

This is the code of my view which inherit of ListAPIView. Here I am creating an endpoint /api/post/ in where the ajax GET request is sent to retrieve the user posts

In this view I manage two scenaries:

a. I get the request of user to view their posts b. I request a list of users which I am following and retrieve their posts

class PostListAPIView(generics.ListAPIView):
    serializer_class = PostModelSerializer
    pagination_class = StandardResultsPagination

    def get_queryset(self, *args, **kwargs):

        # Capturamos el request de un usuario
        requested_user = self.kwargs.get("email")
        if requested_user:

            # Para ver los posts mios y los que reposteo
            qs = Post.objects.filter(user__email=requested_user).order_by('-timestamp')
            query = self.request.GET.get("q", None)
            if query is not None:
                # Para buscar por usuario y por su contenido
                qs = qs.filter(
                    Q(content__icontains=query) |
                    Q(user__email__icontains=query)
                )
            return qs
        else:
            im_following = self.request.user.profile.get_following() 
            # Mostrando los posts de los usuarios que sigo
            # Bind querysets 
            # Para ver los posts de los que sigo y los mios
            qs1 = Post.objects.filter(user__in=im_following)
            qs2 = Post.objects.filter(user = self.request.user)
            qs = (qs1 | qs2).distinct().order_by('-timestamp')
            # print(self.request.GET)
            query = self.request.GET.get("q", None)
            if query is not None:
                qs = qs.filter(
                    Q(content__icontains=query) |
                    Q(user__email__icontains=query)
                )
            return qs

From my html template, I perform the ajax GET request of this way:

{% extends "layout.html" %}
{% block script %}
    // Codigo basico para traernos datos de REST
    <script>
    $(document).ready(function(){
        console.log("working");
        $.ajax({
            url: "/api/post/", 
            method: "GET",
            success: function (data) {
                console.log("the data are")
                console.log(data)

            },
            error: function(data){
                console.log("error")
                console.log(data)
            }
        })
    });
    </script>
{% endblock script %}

I appreciate highly some orientation about it.

bgarcial
  • 2,915
  • 10
  • 56
  • 123
  • 1
    I would recommend setting up better logging in your application so that you can see a full traceback of the 500 error. You could setup email logging to email any errors to you. [See this page](https://docs.djangoproject.com/en/1.11/topics/logging/#configuring-logging) – roob Sep 01 '17 at 17:39
  • 1
    Instead you could use a service like [Sentry](https://sentry.io/). It is free for a single user and really easy to setup. It will send you emails for errors and has a very slick interface for navigating tracebacks. – roob Sep 01 '17 at 17:41
  • 1
    But for this problem, the easiest and fastest thing you could do is set DEBUG = True in your settings for the heroku django server, so you get tracebacks in your browser window. Now make the request through the browser rather than AJAX, and you should get a traceback of the 500 error. Update your question after you have done this. – roob Sep 01 '17 at 17:45
  • @rawbeans Thanks for the information about of [Sentry](https://sentry.io/welcome/) and [logging](https://docs.djangoproject.com/en/1.11/topics/logging/#configuring-logging). In addition, I change to `DEBUG=True`, in my `settings/production.py` but, like the error is reflected in the console browser, Django does not get this `error 500` or at least I don't know how to traceback the error with `DEBUG=True` – bgarcial Sep 01 '17 at 19:46
  • @rawbeans When you tell me **"Now make the request through the browser rather than AJAX, and you should get a traceback of the 500 error"** How to can make the request through the browser? Refreshing it? I am requesting tese data via `GET` from ajax and these data are fetchs when the web page with `/post/` url is reached – bgarcial Sep 01 '17 at 19:48
  • 1
    Currently you are calling the url via ajax. To visit that url directly through the browser, enter the url (`hostayni.herokuapp.com/api/post?q=` not `hostayni.herokuapp.com/post/`) in your browser bar. – roob Sep 01 '17 at 19:54
  • 1
    @rawbeans I had serialized a field, which is not serializable by default with Django Rest Framework. Then, the browser show me this error: `TypeError at /api/post/ Country(code='') is not JSON serializable.` – bgarcial Sep 01 '17 at 21:15
  • 1
    @rawbeans This field is of `django_countries` package, and in this [issue](https://github.com/SmileyChris/django-countries/issues/106 ) there is some solutions about it. I appreciate your time and support, if you want, create an answer with the tips, recommendations and actions which you already tell me and I will mark like answer – bgarcial Sep 01 '17 at 21:17
  • Great! glad you solved this issue. I would still recommend setting up more permanent logging for production as it will save you time in the future! – roob Sep 01 '17 at 21:39

1 Answers1

1

Mostly likely the problem you are having will not be solved by moving to a different hosting provider.

What you should do is find a way to see the full traceback of the error you are getting at your API url. You can do this in one of several ways:

  • Setup django logging to receive traceback emails when errors occur
  • Setup a 3rd party logging service such as Sentry
  • (Easiest) Visit the API url directly in your browser instead of calling via AJAX. You will also need to set DEBUG=True in your settings to see the traceback in the browser
roob
  • 2,419
  • 3
  • 29
  • 45
  • Do you recommend me include logging Django functionality before that Sentry? Although ... Together are not excluding really? – bgarcial Sep 01 '17 at 21:43