Django: simulate HTTP requests in shell

冷暖自知 提交于 2019-12-31 12:22:26

问题


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.


回答1:


How I simulate requests from the python command line is:

  • Use the excellent requests library
  • Use the django reverse function

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 



回答2:


You can use RequestFactory, which allows

  • inserting a user into the request

  • inserting an uploaded file into the request

  • sending specific parameters to the view

Note that you have to specify both the URL and the view class, so it takes an extra line of code than using requests.

from django.test import RequestFactory

request_factory = RequestFactory()
my_url = '/my_full/url/here'  # Replace with your URL -- or use reverse
my_request = request_factory.get(my_url)
response = MyClasBasedView.as_view()(my_request)  # Replace with your view
response.render()
print(response)

To set the user of the request, do something like my_request.user = User.objects.get(id=123) before getting the response.

To send parameters to a class-based view, do something like response = MyClasBasedView.as_view()(my_request, parameter_1, parameter_2)

Extended Example

Here's an example of using RequestFactory with these things in combination

  • HTTP POST (to url url, functional view view, and a data dictionary post_data)

  • uploading a single file (path file_path, name file_name, and form field value file_key)

  • assigning a user to the request (user)

  • passing on kwargs dictionary from the url (url_kwargs)

SimpleUploadedFile helps format the file in a way that is valid for forms.

from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import RequestFactory

request = RequestFactory().post(url, post_data)
with open(file_path, 'rb') as file_ptr:
    request.FILES[file_key] = SimpleUploadedFile(file_name, file_ptr.read())
    file_ptr.seek(0)  # resets the file pointer after the read
    if user:
        request.user = user
    response = view(request, **url_kwargs)



回答3:


(See tldr; down)

Its an old question, but just adding an answer, in case someone maybe interested.

Though this might not be the best(or lets say Django) way of doing things. but you can try doing this way.

Inside your django shell

>>> import requests
>>> r = requests.get('your_full_url_here')

Explanation: I omitted the reverse(), explanation being, since reverse() more or less, finds the url associated to a views.py function, you may omit the reverse() if you wish to, and put the whole url instead.

For example, if you have a friends app in your django project, and you want to see the list_all() (in views.py) function in the friends app, then you may do this.

TLDR;

>>> import requests
>>> url = 'http://localhost:8000/friends/list_all'
>>> r = requests.get(url)


来源:https://stackoverflow.com/questions/11756800/django-simulate-http-requests-in-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!