[Django] (2) create an application using the Django Module

Source: Internet
Author: User
  • 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

  •  Define model models. py

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.

  • Activate 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

  • Manage websites

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.