[PYTHON] Define a division value in Django and easily reflect it on the screen

Manage partition values with Django

Requirements

Define the division value in model.py and reflect it on the html page that displays the history list.

1. Define in the model

Give the History table a partition value of STATUS. Point: choices =

manage.py


class History(models.Model):
    STATUS = (
        ('001', 'Before implementation'),  # BEFORE_EXECUTE
        ('011', 'Login'),  # DURING_LOGIN
        ('012', 'Acquiring'),  # WHILE_GETTING
        ('101', 'Done'),  # DONE
        ('401', 'Failure')  # fail
    )
    
    start_at = models.DateTimeField()
    status = models.CharField(max_length=3, choices=STATUS)

2. Define the URL

Define the URL to the page that displays the list of history.

url.py


urlpatterns = [
    #list
    path('history_list', views.HistoryListView.as_view(), name='history_list'),
]

3. Define in ListView

Define View to be passed to html of the page that displays the history list.

class HistoryListView(ListView):
    model = History
    template_name = 'history_list.html'

4. Reflect in Html

Point: get_FIELD_diaplay

example.html


<td>
    {{ history.get_status_display }}
</td>

Recommended Posts

Define a division value in Django and easily reflect it on the screen
If you define a method in a Ruby class and define a method in it, it becomes a method of the original class.
I want to create a pipfile and reflect it in docker
[Python] The role of the asterisk in front of the variable. Divide the input value and assign it to a variable
How to return the data contained in django model in json format and map it on leaflet
I made a POST script to create an issue on Github and register it in the Project
Enclose the cat result in double quotes and put it in a variable
[Personal memo] Get data on the Web and make it a DataFrame
Find it in the procession and edit it
Create a Python image in Django without a dummy image file and test the image upload
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
Create a Django project and application in a Python virtual environment and start the server
Isn't there a default value in the dictionary?
The result of making a map album of Italy honeymoon in Python and sharing it
[Selenium] Open the link in a new tab and move it [Python / Chrome Driver]
It is difficult to install a green screen, so I cut out only the face and superimposed it on the background image
Specify the volume on linux and make a sound
Read the csv file and display it in the browser
Make the model a string on a Django HTML template
Write a log-scale histogram on the x-axis in python
Transit to the update screen with the Django a tag
How to use Decorator in Django and how to make it
[Django 2.2] Sort and get the value of the relation destination
Get a participant's username and screen name in Slack
Install django on python + anaconda and start the server
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
Get the matched string with a regular expression and reuse it when replacing on Python3
A script that opens the URLs written in CSV in order and takes a full screen screenshot
Recursively get the Excel list in a specific folder with python and write it to Excel.
A story that makes it easier to see Model debugging in the Django + SQLAlchemy environment
An easy way to view the time taken in Python and a smarter way to improve it
Put Ubuntu in Raspi, put Docker on it, and control GPIO with python from the container
Define a task to set the fabric env in YAML
A note on the default behavior of collate_fn in PyTorch
Save the pystan model and results in a pickle file
Temporarily save a Python object and reuse it in another Python
How to generate a query using the IN operator in Django
How to get the last (last) value in a list in Python
Django --Overview the tutorial app on Qiita and add features (2)
Notes on coloring by value in the matplotlib scatter plot
In the python dictionary, if a non-existent key is accessed, initialize it with an arbitrary value
Hannari Python At the LT meeting in December, I made a presentation on "Python and Bayesian statistics".
I want to create a histogram and overlay the normal distribution curve on it. matplotlib edition
A simple reason why the return value of round (2.675,2) is 2.67 in python (it should be 2.68 in reality ...)
For the time being, the one who creates a homepage with Django at the speed of a second and publishes it on Heroku (Windows compatible)