I would like to create a simple Django application with Ext JS interface. Task: The user enters data into a database using forms and sees the database upgrade. It has been done to solve the problem as follows. Written Django application.
views.py:
def addProduct(request):
if request.method == 'POST':
form = ProductForm(request.POST)
if form.is_valid():
data = form.cleaned_data
product_name = data['product_name']
product_price = data['product_price']
Products.objects.create(name=product_name, price=product_price)
else:
form = ProductForm()
return render(request, 'addProduct.html', {
'ProductsList': Products.objects.all(),
'form': form,
# 'Title': 'Range of products',
# 'product_name': 'type product name here',
})
template:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ Title }}</title>
</head>
<body>
<table>
<tr>
<th>Name</th>
<th>Product</th>
</tr>
{% for current in ProductsList %}
<tr>
<td>{{ current.name }}</td>
<td>{{ current.price }}</td>
</tr>
{% endfor %}
</table>
<form action="check/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="ADD" />
</form>
</body>
</html>
forms.py:
class ProductForm(forms.Form):
product_name = forms.CharField(max_length=100)
product_price = forms.IntegerField()
models.py:
class Products(models.Model):
name = models.CharField(max_length=30)
price = models.IntegerField()
This works well.
Now I would like to use Ext.grid.Panel component from Ext JS to render table. Question: How do I transfer data from Django to Ext JS?
UPDATE
I wrote a simple grid, but it turns out the table is empty. What am I doing wrong?
Ext.onReady(function () {
Ext.define('Products', {
extend: 'Ext.data.Model',
fields: [{
name: 'Name',
type: 'string'
}, {
name: 'Price',
type: 'int'
}]
});
var store = Ext.create('Ext.data.Store', {
model: 'Products',
autoLoad: true,
proxy: {
type: 'rest',
url: 'http://127.0.0.1:8000/products/',
reader: {
type: 'json'
}
}
});
Ext.create('Ext.grid.Panel', {
title: 'Products',
height: 200,
width: 400,
store: store,
columns: [{
header: 'Name',
dataIndex: 'Name'
}, {
header: 'Price',
dataIndex: 'Price'
}],
renderTo: Ext.getBody()
});
});
I also updated the Django project using Django Rest Framework for REST technology:
class ProductsList(generics.ListCreateAPIView):
queryset = Products.objects.all()
serializer_class = ProductsSerializer
class ProductDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Products.objects.all()
serializer_class = ProductsSerializer