Commonly used commands in django

高洛峰
Release: 2017-03-01 13:53:51
Original
1230 people have browsed it

Django basic commands

This section is mainly to let you know some of the most basic commands of django. Please try to remember them and practice more

1. New A django project

django-admin.py startproject project-name
Copy after login

A project is a project, project-name project name, change it to your own, it must comply with Python's variable naming rules (Start with an underscore or a letter)

2. Create a new app

python manage.py startapp app-name
或 django-admin.py startapp app-name
Copy after login

Generally, a project has multiple apps, of course they are universal Apps can also be used in multiple projects.

3. Synchronize database

python manage.py syncdb
 
注意:Django 1.7.1及以上的版本需要用以下命令
python manage.py makemigrations
python manage.py migrate
Copy after login

This method can create a table when you add a new class in models.py When you run it, the table will be automatically created in the database without having to create it manually.

Note: To modify existing models, versions of Django before 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, it will automatically restart after modifying the code, which is convenient for debugging and development. However, due to performance issues, it is recommended to only use it for testing and not Used in production environment.

python manage.py runserver
 
# 当提示端口被占用的时候,可以用其它端口:
python manage.py runserver 8001
python manage.py runserver 9999
(当然也可以kill掉占用端口的进程)
 
# 监听所有可用 ip (电脑可能有一个或多个内网ip,一个或多个外网ip,即有多个ip地址)
python manage.py runserver 0.0.0.0:8000
# 如果是外网或者局域网电脑上可以用其它电脑查看开发服务器
# 访问对应的 ip加端口,比如 http://www.php.cn/:8000
Copy after login

5. Clear the database

python manage.py flush
Copy after login

This command will ask yes Yes or no, choosing yes will clear all the data, leaving only an empty table.

6. Create super administrator

python manage.py createsuperuser
 
# 按照提示输入用户名和对应的密码就好了邮箱可以留空,用户名和密码必填
 
# 修改 用户密码可以用:
python manage.py changepassword username
Copy after login

7. Export data Import data

python manage.py dumpdata appname > appname.json
python manage.py loaddata appname.json
Copy after login

For details on data operations, see: Data import and data migration. Now you just need to understand how to use it.

8. Django project environment terminal

python manage.py shell
Copy after login

If you install bpython or ipython, their interface will be automatically used. It is recommended to install it. bpython.

The difference between this command and running python or bpython directly to enter the shell is that you can call the API in models.py of the current project in this shell. There are also some small tests that are very convenient for operating data.

9. Database command line

python manage.py dbshell
Copy after login

Django will automatically enter the database set in settings.py, if it is MySQL or postgreSQL will ask for the database user password.

In this terminal you can execute database SQL statements. If you are familiar with SQL, you may like this approach.

10. More commands

Enter python manage.py on the terminal to see a detailed list, which is especially useful when you forget the subname.

The above detailed explanation of commonly used commands in Django is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website.

For more articles related to common commands in Django, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!