Azure Table Storage Service (https://azure.microsoft.com/en-us/documentation/articles/storage-python-how-to-use-table-storage/) stores a large amount of structured data. I will. This service is a ** NoSQL datastore ** that accepts authenticated calls from inside and outside the Azure cloud. The point is that you can store SQL data externally. Using PTVS This is useful when you want to use SQL with Django.
We will create it by referring to the site of here. First, create a new Django app. Please refer to here for the creation method. Please download the ** azure ** package with pip using ** Install Python Package **.
At the Azure Portal (https://portal.azure.com/) Select ** [Storage Account] ➡︎ [Add] **. Basically, you can decide only the ** name ** part and then use the default settings. Also, if you have not created a ** resource group **, create one!
Once created, the storage account will be from the account you just created ** [Account] ➡︎ [Access key] ➡︎ [Storage account / key] ** Let's remember! Required when accessing from the outside.
I referred from the site introduced first. I will omit the explanation of imported azure, but if you want to know more, [here](https://github.com/yuesan/azure-content-jajp/blob/master/articles/storage/storage-python-how- There is an explanation in to-use-table-storage.md).
views.py
from django.http import HttpRequest
from django.template import RequestContext
from datetime import datetime
from django.http import HttpResponse
from django.template.loader import render_to_string
from azure.storage.table import TableService, Entity
account_name = 'Storage account name'
account_key = 'key(Any of multiple keys is fine)'
table_service = TableService(account_name=account_name, account_key=account_key)
table_service.create_table('mytasks')
def list_tasks(request):
entities = table_service.query_entities('mytasks', '', 'name,category')
html = render_to_string('app/test.html', {'entities':entities})
return HttpResponse(html)
def add_task(request):
name = request.GET['name']
category = request.GET['category']
table_service.insert_entity('mytasks', {'PartitionKey':name+category, 'RowKey':name, 'name':name, 'category':category})
entities = table_service.query_entities('mytasks', '', 'name,category')
html = render_to_string('app/test.html', {'entities':entities})
return HttpResponse(html)
def update_task(request):
name = request.GET['name']
category = request.GET['category']
partition_key = name + category
row_key = name
table_service.update_entity('mytasks', partition_key, row_key, {'PartitionKey':partition_key, 'RowKey':row_key, 'name': name, 'category':category})
entities = table_service.query_entities('mytasks', '', 'name,category')
html = render_to_string('app/test.html', {'entities':entities})
return HttpResponse(html)
test.html
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>My Tasks</h2> <br>
<table border="1">
<tr>
<td>Name</td>
<td>Category</td>
</tr>
{% for entity in entities %}
<form action="update_task" method="GET">
<tr>
<td>{{entity.name}} <input type="hidden" name='name' value="{{entity.name}}"></td>
<td>{{entity.category}}
<input type="hidden" name='category' value="{{entity.category}}"></td>
</tr>
</form>
{% endfor %}
</table>
<br>
<hr>
<table border="1">
<form action="add_task" method="GET">
<tr>
<td>Name:</td
><td><input type="text" name="name"></input></td>
</tr>
<tr>
<td>Category:</td>
<td><input type="text" name="category"></input></td>
</tr>
<tr>
<td><input type="submit" value="add task"></input></td>
</tr>
</form>
</table>
</body>
</html>
urls.py
urlpatterns = [
url(r'^$', 'app.views.list_tasks'),
url(r'^list_tasks$', 'app.views.list_tasks'),
url(r'^add_task$', 'app.views.add_task'),
url(r'^update_task$', 'app.views.update_task')
When you execute it, there are ** Name: ** and ** Category **, so fill in each and add task will store the value in ** My Tasks **. You can also re-fill it or restart Django, of course, but the values will be preserved.
You can see that the value is actually inserted from the storage account. It's very difficult and annoying to actually install SQL in Windows, so I think it's a very nice feature, so please give it a try.
Recommended Posts