Django's Common commands

Source: Internet
Author: User
Django basic Commands

This section is designed to let you understand some of the most basic Django commands, please try to remember them, and practice a lot

1. Create a new Django project

django-admin.py Startproject Project-name

A project, project-name the project name, to your own, to conform to the Python variable naming convention (the beginning of an underscore or letter)

2. Create a new app

Python manage.py startapp app-name or django-admin.py startapp app-name

Typically, a project has multiple apps, and of course a common app can be used in multiple projects.

3. Synchronizing the database

Python manage.py syncdb Note: Django 1.7.1 and above need to use the following command Python manage.py makemigrationspython manage.py Migrate

This method creates a table, and when you add a class to the models.py, you can run it to automatically create a table in the database without creating it manually.

Note: For existing models, Django before Django 1.7 cannot change the table structure automatically, but there are third-party tools south, see the Django Database Migration section.

4. Using the development server

Development server, that is, the development of the use of general code changes will be automatically restarted, convenient debugging and development, but due to performance issues, it is recommended only for testing, do not use in the production environment.

Python manage.py runserver # When the tip port is occupied, you can use a different port: Python manage.py runserver 8001python manage.py runserver 9999 (You can also kill the process that occupies the port) # Listen to all available IPs (the computer may have one or more intranet IPs, one or more extranet IPs, i.e. multiple IP addresses) Python manage.py runserver 0.0.0.0:8000# If it is an extranet or LAN computer can use other computers to view the development server # access to the corresponding IP plus port, such as http://www.php.cn/:8000

5. Clear the Database

Python manage.py Flush

This command asks whether yes or no, select Yes to erase the data, leaving only the empty table.

6. Create a Super Administrator

Python manage.py Createsuperuser # Follow the prompts to enter the user name and the corresponding password is OK The mailbox can be left blank, user name and password required # Modify user password can be used: Python manage.py ChangePassword Username

7. Export Data Import data

Python manage.py dumpdata appname > Appname.jsonpython manage.py loaddata Appname.json

For data operations See: Data import data Migration, now that you know how to use it.

8. Django Project Environment Terminal

Python manage.py Shell

If you installed Bpython or Ipython will automatically use their interface, it is recommended to install Bpython.

The difference between this command and the direct run of Python or bpython into the shell is that you can invoke the API from the current project's models.py in this shell, and there are some small tests handy for manipulating the data.

9. Database command line

Python manage.py Dbshell

Django automatically enters the database set up in settings.py and, if it is MySQL or PostgreSQL, requires a database user password.

The SQL statement of the database can be executed at this terminal. If you're familiar with SQL, you might like this way.

10. More Commands

Enter Python manage.py on the terminal to see a detailed list, especially useful when you forget the sub-name.

The above-mentioned General Django command is a small part of the whole content to share to everyone, I hope we can give you a reference, but also hope that we support a lot of topic.alibabacloud.com.

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.