Far
Far

Reputation: 450

how to convert httpresponse object to dictionary

first of all i'm sorry because of my duplicated question but actually the other didn't work for me at all.

my problem is that I have 2 views which the first one is returning a Httpresponse to the 2nd, and what I want is to convert this Httpresponse to dictionary in the 2nd view and have access to it's elements.

here is the code :

1st view:

def base_tag_upload(request, tag):

error = False
upload_msg = "success"
user = request.user
response_data = {"error": error, "upload_msg": upload_msg, "file_id": None, "file_version": None}

if request.method == 'POST':
    form = UploadForm(request.POST or None, request.FILES or None, tag=tag, user=user)
    if form.is_valid():
        cd = form.cleaned_data
        uploaded_file = cd['file']
        collection_name = cd['new_name'] or uploaded_file.name.split('.')[0].strip()
        response_data.update(
            {"uf_name": uploaded_file.name, "uf_size": uploaded_file.size, "uf_colname": collection_name})
        set_primary = True  # first file in collection

        # Finding/Creating Related FileCollection
        collection = FileCollection.objects.create(name=collection_name)
        is_major = cd['version_type'] == 'mj'
        file_obj = collection.upload_file(uploaded_file, set_primary, Major_version=is_major)
        file_obj.author = user
        collection.tags.add(tag)
        collection.get_tag_permissions(tag, False)
        file_obj.get_collection_permissions(collection, False)
        set_user_ownership(collection, tag, user)
        set_user_ownership(file_obj, tag, user)

        collection.save()
        file_obj.collection = collection
        file_obj.save()
        response_data.update({'file_id':file_obj.id, 'file_version': file_obj.version})
        ActionLog.log(action=Action.objects.get(name="create"), target=file_obj,
                      user=user, request=request, details=None, extra_details=None)
        redirect_url = reverse('file_history', kwargs={'collection_id': collection.id})
        response_data.update({'redirect': redirect_url})
        return HttpResponse(json.dumps([response_data]))

and the 2nd one :

def tag_upload(request, tag_id):
try:
    tag = Tag.objects.get(id=tag_id)
except Tag.DoesNotExist:
    return HttpResponse(simplejson.dumps([{'error': 'value_error', 'upload_msg': "no such folder"}]))
k = base_tag_upload(request, tag)
k.response.decode('utf-8')
print k
return base_tag_upload(request, tag)

but I got this error when I wanted to decode the Httpresponse as shown above :

AttributeError: 'HttpResponse' object has no attribute 'response'

Upvotes: 0

Views: 1606

Answers (2)

bruno desthuilliers
bruno desthuilliers

Reputation: 77912

I have 2 views which the first one is returning a Httpresponse to the 2nd

Then you didn't structured your code properly - a view has no business calling another view not messing with the response.

If you need to share some common code between two views, then extract this code in a distinct function (that would in this case return a plain dict) and call this function from both your views.

Upvotes: 1

xfx
xfx

Reputation: 1948

It's k.content.decode('utf-8').

Upvotes: 0

Related Questions