I will explain how to use SQLite. SQLite is the same relational database as MySQL, but it can be run as a standalone application instead of running as a server. Reference SQL is the default setting in Django!
I would like to port the previous app to SQLite. If you don't know PTVS, please refer to here to create a project!
This is the main task part!
views.py
# -*- coding: utf-8 -*-
"""
Definition of views.
"""
from django.shortcuts import render
from django.template.loader import render_to_string
from django.http import HttpResponse
from django.http import HttpRequest
from django.template import RequestContext
from datetime import datetime
from app.models import Check_list
def list_tasks(request):
entities = Check_list.objects.all()
html = render_to_string('app/test.html', {'entities':entities})
return HttpResponse(html)
def add_task(request):
name = request.GET['name']
category = request.GET['category']
Check_list.objects.update_or_create(name=name,category=category)
entities = Check_list.objects.all()
html = render_to_string('app/test.html', {'entities':entities})
return HttpResponse(html)
def update_task(request):
name = request.GET['name']
category = request.GET['category']
Check_list.objects.filter(name=name,category=category).delete()
entities = Check_list.objects.all()
html = render_to_string('app/test.html', {'entities':entities})
return HttpResponse(html)
The part that calls the function by URL
urls.py
"""
Definition of urls for DjangoWebProject4.
"""
from datetime import datetime
from django.conf.urls import url
import django.contrib.auth.views
import app.forms
import app.views
# Uncomment the next lines to enable the admin:
from django.conf.urls import include
#admin
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
# Examples:
#admin
url(r'^admin/', include(admin.site.urls)),
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)
]
Database settings Here, Name and Category columns are set in the table called Check_list.
models.py
"""
Definition of models.
"""
from django.db import models
# Create your models here.
class Check_list(models.Model):
name = models.CharField('Name', max_length=255)
category = models.CharField('Category', max_length=255, blank=True)
Admin screen display If you don't ** list_display ** here, it will be in the database part of Django Check_list is not displayed even if I access it
admin.py
from django.contrib import admin
from app.models import Check_list
class Check_listAdmin(admin.ModelAdmin):
list_display = ('id', 'name', 'category')
list_display_links = ('id', 'name',)
admin.site.register(Check_list, Check_listAdmin)
Ordinary HTML with tables
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>
<td>Check</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>
<td><input type="submit" value="Delete"></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>
Right click on the project 【Python】▶︎【Django Create Superuser】 To select This allows you to create a superuser and log in to Django's Admin screen!
When I enter the password when creating it, it seems that an error occurs, but it just means that I will enter the password again, so please create a super user
Next, to reflect the Database settings 【Python】▶︎【Django Make Migrations】 【Python】▶︎【Django Make Migrate】 Execute and reflect each! Also, if you add or change a database If you do not execute it in the same way, it will not be reflected!
** RUN ** launches an app that can be deleted and added You can also access the Django database by ** / admin ** and see the table contents in the GUI!
Select [Web Apps] from [Publish] and deploy on Azure in the same way as here. After deploying ** [App Service] ▶ ️ [Overview] ▶ ️ [URL] ** You can confirm that the previous application is working by opening from
** * You can use SQL without having to use SQL data storage! ** **
I was surprised that SQLite can be used as it is even if it is uploaded to Azure! However, I tried MySQL, but it didn't work after all, so I want to do something about it! Please let me know if you are familiar with it! !!
Recommended Posts