[PYTHON] Create a shogi game record management app using Django 3 ~ Django default management site settings ~

Introduction

This is ** 3rd **, a memorandum of making a game record management app for shogi using Django.

Work environment

The working environment this time is as follows

Also, Django's directory structure looks like this:

- kifu_app_project/
    - kifu_app_project/
        - __init__.py
        - setting.py
        - urls.py
        - wsgi.py
    - kifu_app/
        - migrations/
        - __init__.py
        - admin.py
        - apps.py
        - models.py
        - tests.py
        - views.py
    - manage.py
    - .gitignore

Contents of this article

--Django default admin site settings

Django default admin site settings

Django comes standard with a management site for viewing and managing DB data. This time, we will make settings to use this management site.

Edit admin.py

Edit admin.py in kifu_app.

admin.py


from django.contrib import admin

# Register your models here.

#Added below

from .models import LargeClass, MiddleClass, SmallClass, Information, Kifu

admin.site.register(LargeClass)
admin.site.register(MiddleClass)
admin.site.register(SmallClass)
admin.site.register(Information)
admin.site.register(Kifu)

Import all the classes created last time in models.py. Then pass all the imported classes as arguments to ʻadmin.site.register () `.

Create user for login

Next, go to the point where you can see manege.py and type the following command.

$ python manage.py createsuperuser
Username: <username>
Email address: <Email>
Password: <password>

Enter the required information for the settings. You don't have to enter an email.

Launch a local server

This is the third serialization, but I will finally launch the server. In the directory where you can see manage.py, type the following command

$ python manage.py runserver

Try accessing the address listed in Starting development server at ~. Normally it should be localhost: 8000. If a screen like the one in the picture below appears, it's OK! main.png

Log in to the management site

Then access / admin / from the current URL. (Usually localhost: 8000 / admin /) Then, enter the user name and password you set earlier to log in.

Probably, it will transition to the screen like the picture below. admin.png

Customize management site

It seems that the management site can be changed to your own by changing CSS etc.

Here, we will make the settings easier to use.

Insert new data

First, try entering some data. You can easily insert it by pressing the add button.

Changed models.py to return data contents

However, if you look at the inserted data from the list, you can only see that it is * Object *, and you have to check the details one by one. largeClass.png

So, add a __str__ method to each class in models.py.

models.py


from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator

# Create your models here.

class LargeClass(models.Model):
    name = models.CharField(max_length=10)

    def __str__(self):      #add to
        return self.name

class MiddleClass(models.Model):
    large_class = models.ForeignKey(LargeClass, on_delete=models.CASCADE)
    name = models.CharField(max_length=10)

    def __str__(self):     #add to
        return self.name

class SmallClass(models.Model):
    middle_class = models.ForeignKey(MiddleClass, on_delete=models.CASCADE)
    name = models.CharField(max_length=10)

    def __str__(self):     #add to
        return self.name

class Information(models.Model):
    date = models.DateTimeField()
    sente = models.CharField(max_length=50)
    gote = models.CharField(max_length=50)
    result = models.IntegerField(validators=[MinValueValidator(0), MaxValueValidator(3)])
    my_result = models.IntegerField(validators=[MinValueValidator(0), MaxValueValidator(3)])
    small_class = models.ForeignKey(SmallClass, on_delete=models.CASCADE)
    create_at = models.DateTimeField(auto_now_add=True)
    update_at = models.DateTimeField(auto_now=True)

    def __str__(self):     #add to
        # return self.date     <- self.Since date is of type datetime, this is an error
        return self.date.strftime("%Y/%m/%d_%H:%M:%S")    #In this way, convert it to str type and return

class Kifu(models.Model):
    information = models.ForeignKey(Information, on_delete=models.CASCADE)
    number = models.IntegerField(validators=[MinValueValidator(0)])
    te = models.CharField(max_length=20)

    def __str__(self):     #add to
        return self.te

Use the __str__ () method to return the variable of the column you want to display. Now when I refresh my browser, the information is displayed safely. largeClass2.png

There are many other customizations you can make, so please refer to the following sites. Python Django Tutorial (2) Creating your first Django app, part 7

If you forget the password of the administrator user (Added on 2020/03/09)

When I tried to use the admin function for the first time in a long time, I forgot the password, so I investigated how to change it.

Type the following command at the command prompt to change it.

$ python manage.py changepassword <Username you want to change>
Changing password for user <>
Password:
Password(again):
Password changed successfully for user <>

This will change your password.

Also, if you don't know your username, you can find it by logging in to your DB (mysql in my case) and checking the auth_user table of the DBs used in this project. (I don't know because the password is hashed.)

The following is a reference site. How to reset Django admin password?

Next time preview

[Create View] 1

Recommended Posts

Create a shogi game record management app using Django 3 ~ Django default management site settings ~
Create a shogi game record management app using Django 2-Database settings-
Create a shogi game record management app using Django 4 ~ Create View ~
Create a shogi game record management app using Django 6 ~ Template division ~
Creating a Shogi Game Record Management App Using Django 1-Environment Construction-
Create a shogi game record management application using Django 5 ~ Pass DB data to Template ~
Try to create a Todo management site using WebSocket with Django (Swamp Dragon)
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 1 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 2 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 3 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 4 ~
[CRUD] [Django] Create a CRUD site using the Python framework Django ~ 5 ~
Create and deploy a Django (PTVS) app using Azure Table storage
Create a beauty pageant support app using PyLearn2
django default settings
Create a Mac app using py2app and Python3! !!
Until you create a new app in Django
Create a Todo app with the Django REST framework
Create a Todo app with Django ③ Create a task list page
To myself as a Django beginner (1) --Create a project app--
To myself as a Django beginner (4) --Create a memo app--
Create a Todo app with Django ⑤ Create a task editing function
Create a Django schedule
Create a game to control puzzle & dragons drops using pygame
[Learning record] Create a mysterious dungeon game with Pyhton's Tkinter
Create a Todo app with Django ① Build an environment with Docker
Create a simple CRUD app using Django's generic class view
Create a homepage with django
DEBUG settings when using Django
Create a Django login screen
Web App Development Practice: Create a Shift Creation Page with Django! (Shift creation page)