Reputation: 33
My models.py is as follows:
class SellerMarketplaces(models.Model):
id = models.IntegerField(db_column='ID',primary_key=True) # Field name made lowercase.
seller_id = models.ForeignKey('Sellerdetails',db_column='Seller_ID') # Field name made lowercase.
mk_id = models.ForeignKey('Marketplace',db_column='Mk_ID') # Field name made lowercase.
username = models.CharField(db_column='Username', max_length=100, blank=True, null=True)
I have the following code in my views
def marketplaces(request):
seller = request.GET.get('seller', '')
allmarketplaces = SellerMarketplaces.objects.filter(seller_id = seller).values('mk_id__marketplace')
for i in range(len(allmarketplaces)):
print allmarketplaces[i]['mk_id__marketplace']
return render_to_response(request,'soumi1.html',{'marketplaces':marketplaces})
I am getting the values (Flipkart & Snapdeal) from the print allmarketplaces[i]['mk_id__marketplaces] line, but the return is giving me a 500 error.
My html file is:
<select name="marketplace" id="txtHint" size="1" required>
<option value="">Select Marketplace</option>
{% if marketplaces.count > 0 %}
{% for marketplace in marketplaces %}
<option value = {{ marketplace.id }}>{{ marketplace.marketplace }} </option>
}
{% endfor %}
{% endif %}
the javascript i used is:
function showUser(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET",'/marketplaces?seller='+username['value']);
xmlhttp.send();
}
Upvotes: 0
Views: 1348
Reputation: 6488
This line is causing the trouble:
return render_to_response(request,'soumi1.html',{'marketplaces':marketplaces})
You are returning marketplaces
but your view is also called marketplaces
.
Taking a guess I'd say you want to return the list of marketplaces.
You can do that in the following way:
marketplaces_list = SellerMarketplaces.objects.filter(seller_id=seller).values_list('mk_id__marketplace', flat=True)
return render_to_response(request,'soumi1.html',{'marketplaces':marketplaces_list})
Upvotes: 1