Django Post Issue

26 views
Skip to first unread message

G Z

unread,
Jun 20, 2014, 1:33:33 PM6/20/14
to django...@googlegroups.com
I'm trying to post
customer_id
while generating a combo box with a loop from a database query.
I have the combo box generating and such, but the posted data does not return the customer id. It returns something weird.

<form method='post' action='' id='customers'>
{% csrf_token %}
<select>
{% for item in customer %}
<option value={{ item.customer_id }} name='customer_id'>{{ item.customer_name }}</option>
{% endfor %}
<input type = 'submit' value='Edit'>
</select>
{{ post }}


this is what it returns
<QueryDict: {u'csrfmiddlewaretoken': [u'CpzKrwmZsmfiiNHngNWDFSNxqUoBykYO']}>


def portal(request):
    
    customers = Customer.objects.all()
    
    
    
    if request.method == 'POST':
       vm_groups = Vm_group.objects.all()
       vms = Vm.objects.all()
       selected_customer = request.POST.copy()
       #selected_customer_id = selected_customer['customer_id']
 
       post = selected_customer
       context = Context({'customer': customers, 'vm_groups':vm_groups, 'vms':vms, 'post':post, 
                                                   })
       
       return render(request, 'portal.html', context)
    
    else:
       context = Context({'customer': customers,})
       return render(request, 'portal.html', context)

    

Nikolas Stevenson-Molnar

unread,
Jun 20, 2014, 2:00:00 PM6/20/14
to django...@googlegroups.com
The "name" attribute should be on the select tag. E.g:

<select name="customer_id">
  <option value="foo">Foo</option>
</select>

_Nik
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e75fd019-a74e-415f-b681-8617a781cf38%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tom Evans

unread,
Jun 20, 2014, 2:00:37 PM6/20/14
to django...@googlegroups.com
On Fri, Jun 20, 2014 at 6:33 PM, G Z <zuk...@gmail.com> wrote:
> I'm trying to post
> customer_id
> while generating a combo box with a loop from a database query.
> I have the combo box generating and such, but the posted data does not
> return the customer id. It returns something weird.
>
>> <form method='post' action='' id='customers'>
>> {% csrf_token %}
>> <select>

Only form elements with names are submitted.

Cheers

Tom

G Z

unread,
Jun 20, 2014, 2:14:18 PM6/20/14
to django...@googlegroups.com, teva...@googlemail.com
Thanks lol its one of those days, I notice i was calling the options the name instead of the select.


this might be a django ism but after i post the number and try to check it with an if statement it doesnt do anythign but if i print both out they are the same here is the code


{% if user.is_authenticated %}
<h1>Enki-Corp says... hello {{ user.username }}!</h1>
{% else %}
<h1>Enki-Corp says... hello world!</h1>
{% endif %}

{% if user.is_authenticated %}


<form method='post' action='' id='customers'>
{% csrf_token %}
<select name='customer_id'>

{% for item in customer %}
<option value={{ item.customer_id }} >{{ item.customer_name }}</option>

{% endfor %}
<input type = 'submit' value='Edit'>
</select>


<table>
    <tr>
        <th>ID</th>
     
        <th>NAME</th>
    </tr>
    <br>

    {% for item in customer %}
{{post}} -  {{item.customer_id}} <br> //where I print them out to check if they match when infact there is a matching number it does nothing. but all my if statements below work.
 
   {% if post == item.customer_id %}
test
<tr>
       <td valign='top'>{{ item.customer_id }}  <a href="{{ item.customer_id }}">{{ item.customer_name }}</a>
       <li>Customer Inactive: {{item.inactive}}</li>
       </td>
       <td>
        {% for group in vm_groups %}
        {% if item.customer_id == group.customer_id %}
        {% for vm in vms %}
        {% if group.vm_group_id == vm.vm_group_id %}
        <ul>
              <li>VM Name: {{vm.vm_name}}</li>
              <ul>
              <li>VM ID: {{vm.vm_id}}</li>
              <li>VM Display Name: {{vm.vm_display_name}}</li>
              <li>VM Group: {{group.vm_group_name}}</li>
              <li>Vm Mor:  {{vm.vm_mor}} </li>
              <li>Vm Datacenter:  {{vm.datacenter_id}} </li>
              <li>VCD Managed:  {{vm.vcd_managed}} </li>
              </ul>
              </ul>

       {%endif%}
   {%endfor%}
        {%endif%}
       {% endfor %}    
   </td>    
</tr>
<tr>
<td colspan='2' align='center'>  </td>
{%endif%}
    {% endfor %}
 
</table>
</form>
<a href="/logout/">Logout</a><br />
{% else %}
You are not authenticated.
<a href="/login/">Login</a><br />
{% endif %}

G Z

unread,
Jun 20, 2014, 3:33:05 PM6/20/14
to django...@googlegroups.com, teva...@googlemail.com
solved it item.customer_id was a int post needed to be passed as an int..
Reply all
Reply to author
Forward
0 new messages