0

I'm a newbie in django and I'm trying to send data (a form) to an external server but so far I haven't succeeded.

This is the simplified version of my view:

def pay(request):    
if request.method == 'POST':
    form = PaymentForm(post_values)
if form.is_valid():       
        return render_to_response('http://externalserver/pay/', RequestContext({'form': form}))

but it gives me the following error:

AttributeError at /pay/ 'dict' object has no attribute 'META'

I don't really know if I should/can use render_to_response. Could anyone help me on this?

2 Answers 2

1

This makes no sense:

render_to_response('http://externalserver/pay/'...)

This method needs a relative path to an html template in your project, on your server.
https://docs.djangoproject.com/en/1.6/topics/http/shortcuts/#django.shortcuts.render_to_response

You need to think about what happens:

The action in the form tag, in the HTML, determines where the data is posted to, eg <form action="" method="post"> means "post this form to the url of the page I'm already on"... in other words the browser posts the data to the Django view which was used to render the form. This is the most common scenario.

You could post the form data directly to the external url:

<form action="http://externalserver/pay/" method="post">

Maybe this is what you want? But then your Django view wouldn't see the data at all.

If you need to process the data in Django, then post the processed data from your server to the external url, you will need to do something different in your view. I recommend using the Requests library for a nice way to make post requests in Python.

Sign up to request clarification or add additional context in comments.

Comments

0

Your question does not make your intentions clear, unfortunately. If you mean to relay the form data between the client and a third-party server and then the server’s response back to the client, then that is referred to as proxying.

It is easily achievable using the requests library. You may find it easier to make use of django-proxy instead of writing your own method, as it would take care of rewriting the request headers (also known as the browser signature) in both directions.

If, on the other hand, you only wish to transfer the contents of the form to a third party server without relaying back the server’s response, then you can simply rely on the requests library and then respond to the client using a local template.

Here is a little example:

def pay(request):
    if request.method == 'POST':
        form = PaymentForm(request.post)
    if form.is_valid():
        response = requests.post(third_party_url, data=request.post)
        'process response'
        render_to_response(success_or_failure_template)
    else:
        render_to_response(bad_data_template)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.