Common django commands and django commands
Django basic commands
This section mainly aims to help you understand some of django's most basic commands. Please remember them and practice them more.
1. Create a django project
django-admin.py startproject project-name
A project is a project, and the project-name project name is changed to your own. It must comply with the Python variable naming rules (starting with an underscore or letter)
2. Create an app
Python manage. py startapp app-name or django-admin.py startapp app-name
Generally, a project has multiple apps. Of course, generic apps can also be used in multiple projects.
3. Synchronize Databases
Python manage. py syncdb Note: For Django 1.7.1 and later versions, run the following command python manage. py makemigrationspython manage. py migrate.
In this way, you can create a table. When you add a class in models. py, you can run it to automatically create a table in the database without manual creation.
Note: If you modify the existing models, Django versions earlier than Django 1.7 cannot automatically change the table structure. However, there is a third-party tool south. For details, see the Django database migration section.
4. Use the Development Server
The development server is used during development. Generally, the code is automatically restarted after modification to facilitate debugging and development. However, due to performance problems, it is recommended that the server be used only for testing and not for production environments.
Python manage. py runserver # When the system prompts that the port is in use, you can use another port: python manage. py runserver 8001 python manage. py runserver 9999 (of course, you can kill the process that occupies the port) # Listen to all available ip addresses (one or more Intranet ip addresses and one or more Internet ip addresses may exist on the computer, there are multiple IP addresses) python manage. py runserver 0.0.0.0: 8000 # if it is an internet or LAN computer, you can use another computer to view the development server # access the corresponding ip address and port, such as http: // 172.16.20.2: 8000
5. Clear the database
python manage.py flush
This command will ask whether it is yes or no. If yes is selected, all data is cleared, leaving only empty tables.
6. Create a super Administrator
Python manage. py createsuperuser # enter the user name and password as prompted. Leave the mailbox blank. the user name and password are required. # You can use python manage. py changepassword username to modify the user password.
7. export data to import data
python manage.py dumpdata appname > appname.jsonpython manage.py loaddata appname.json
For details about data operations, see import data migration. Now you can understand this usage.
8. Django project environment Terminal
python manage.py shell
If you have installed bpython or ipython, the interface will be automatically used. We recommend that you install bpython.
The difference between this command and directly running python or bpython to enter the shell is that you can call the models of the current project in this shell. the APIs in py are very convenient for some small tests on operation data.
9. Database Command Line
python manage.py dbshell
Django will automatically enter the database set in settings. py. If it is MySQL or postgreSQL, it will require you to enter the database user password.
On this terminal, you can execute the SQL statement of the database. If you are familiar with SQL, you may like this method.
10. More commands
Enter python manage. py on the terminal to view the detailed list, which is especially useful when you forget the subname.
The detailed explanation of the Common commands of django is the full content shared by the editor. I hope you can give us a reference and support the help house.