- Keep the runserver running. in the same directory as manage. py, open a new terminal window, activate the virtual environment, and execute the Startapp command.
Python manage. py Startapp learning_logs
At the code level, a model is a class.
Open models. py, write the model topic, and describe the topic of the Learning Log.
From Django. DB import models # create your models here. class topic (models. model): text = models. charfield (max_length = 200) data_added = models. datetimefield (auto_now_add = true) def _ self _ (Self): "returns the model's string representation" return self. text
After adding a model, you must activate the model.
Open the setting. py file in the project.
Add this application
Run Python manage. py makemigrations learning_logs on the terminal
This allows Django to modify the database so that it can store information related to the model topic.
Apply the migration and execute Python manage. py migrate.
(Each time you need to modify the managed data, you need to take these three steps. First modify models. py, then call makemigrations for learning_logs, and then let Django migrate the project)
After the modification, you need Python manage. py makemigrations app_name and then Python manage. py migrate
1. Create a Super User for the website, execute Python manage. py createsuperuser, and enter the user name and password.
2. register the created topic model with the management website in Admin. py.
Access localhost: 8000/admin and enter the created super user name and password. The management interface is displayed.
You can add a chess topic.
Unable to add, the python manage. py migrate is not executed for migration.
Add a model entry to display entries under the topic
class Entry(models.Model): topic = models.ForeignKey(‘Topic‘,on_delete=models.CASCADE) text = models.TextField() data_added = models.DateTimeField(auto_now_add=True) class Meta: verbose_name_plural = ‘entries‘ def __str__(self): return self.text[:50]+‘...‘
Add it to models. py. Pay attention to the method of specifying the foreign key. Otherwise, an error will be reported. Then, migrate the module and register the entry
[Django] (2) create an application using the Django Module