[PYTHON] Application development using SQLite with Django (PTVS)

What is SQLite

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!

App development with 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!

Whole source code

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>

Setting

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

k1.png

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!

k2.png

Run

** 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! 1.png

Deploy

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! ** ** 2.png

At the end

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

Application development using SQLite with Django (PTVS)
WEB application development using Django [Django startup]
WEB application development using Django [Application addition]
WEB application development using Django [Model definition]
WEB application development using Django [Initial settings]
WEB application development using Django [Request processing]
WEB application development using Django [Template addition]
WEB application development using Django [Admin screen creation]
Web application development with Flask
Web application creation with Django
Test Driven Development with Django Part 3
Test Driven Development with Django Part 6
Measure Django application coverage with Coverage.py
Deploy a Django application with Docker
Test Driven Development with Django Part 2
Twitter posting application made with Django
Build a web application with Django
Test Driven Development with Django Part 1
Application development with Docker + Python + Flask
Application development using Azure Machine Learning
Test Driven Development with Django Part 5
WEB application development using django-Development environment construction-
I made a WEB application with Django
Build Django + NGINX + PostgreSQL development environment with Docker
Until Django application creation by terminal (development environment)
[Python] Build a Django development environment with Docker
Django with Python Tools 2.2 for Visual Studio (PTVS 2.2)
Build a Django development environment with Doker Toolbox
Azure External Storage MySQL Challenges with Django (PTVS)
CRUD with Django
Create a one-file hello world application with django
First Django development
I tried using a database (sqlite3) with kivy
Procedure for creating an application with Django with Pycharm ~ Preparation ~
Build a Django development environment using pyenv-virtualenv on Mac
Precautions when using sqlite3 on macOS Sierra (10.12) with multiprocessing
(Python) Try to develop a web application using Django
[Mac OS] Use Kivy with PyCharm! [Python application development]
Web application made with Python3.4 + Django (Part.1 Environment construction)
Build a machine learning application development environment with Python
Single sign-on to your Django application with AWS SSO
A memo about building a Django (Python) application with Docker
Tech-Circle Let's start application development using machine learning (self-study)
Deploy a Django app made with PTVS on Azure
Build a development environment with Poetry Django Docker Pycharm
Model.objects.extra to consider before using raw SQL with Django
Articles that enable system development with Django (Python) _Introduction
Make an application using tkinter an executable file with cx_freeze
Prepare Django development environment using homebrew on MacOSX Mavericks (10.9)
Authenticate Google with Django
Django 1.11 started with Python3.6
Upload files with Django
Django2.2 SQLite3 version error
Output PDF with Django
Markdown output with Django
Use Gentelella with django
Twitter OAuth with Django
Getting Started with Django 1
Send email with Django
File upload with django
Try using Django templates.html