This is ** 3rd **, a memorandum of making a game record management app for shogi using Django.
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
--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 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 () `.
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.
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!
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.
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.
First, try entering some data. You can easily insert it by pressing the add button.
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.
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.
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
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?
[Create View] 1