djangodjango-shell

Django: simulate HTTP requests in shell


I just learnt that with Rails is possible to simulate HTTP requests in the console with few lines of code.

Check out: http://37signals.com/svn/posts/3176-three-quick-rails-console-tips (section "Dive into your app").

Is there a similar way to do that with Django? Would be handy.


Solution

  • How I simulate requests from the python command line is:

    A simple way of simulating requests is:

    >>> from django.urls import reverse
    >>> import requests
    >>> r = requests.get(reverse('app.views.your_view'))
    >>> r.text
    (prints output)
    >>> r.status_code
    200
    

    Update: be sure to launch the django shell (via manage.py shell), not a classic python shell.

    Update 2: For Django <1.10, change the first line to

    from django.core.urlresolvers import reverse