0

I would really appreciate if you can give a hand with this. What I'm trying to do it's just to render the value of some text label, but it gives me the token.

I'm learning django I hope you can comprehend.

The result of this is:

eee 12121 csrfmiddlewaretoken yYvl3neQZSP33vSRNto3FUFa88AMeFQi

view.

def test(request):
    if request.method == "POST":
        response = ''
        for key, value in request.POST.items():
            response += '%s %s\n' % (key, value)

        return HttpResponse(response) 

    return render(request, 'datos2.html')

datos2.

<form action="/test" method="post"> {% csrf_token %}
<input type="text" name="eee">
<input type="submit">
</form>


<p>ADD VALUE</p>

<button onclick="myFunction()">ADD</button>

<script>
function myFunction() {
    var x = document.createElement("INPUT");
    x.setAttribute("type", "text");
    x.setAttribute("value", "0");
    x.setAttribute("name", "eee");
    document.body.appendChild(x);
}
</script>

enter image description here


enter image description here

1
  • The problem is that django is not taking the add labels and I do not know how to make django take them. It only take the <input type="text" name="eee"> and not the others created dynamically :( Commented Oct 8, 2016 at 1:51

1 Answer 1

1

You are looping over all the elements in the POST array in this snippet of code

for key, value in request.POST.items():
    response += '%s %s\n' % (key, value)

I believe, if i understand your question, that what you are after is simply request.POST.get('eee')

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

2 Comments

Hello, thank you for answer, if I get the value like you said works fine, the issue is that I need to add values, that's why I need to get all the elements.
Ah, your question makes a bit more sense with the comment you added to you question above. Two problems, first, the input elements you are dynamically creating in JS, are added to the body, not to the form. Secondly, are giving the dynamically create elements the same name 'eee'.

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.