
Django basic commands Django基本コマンド
pip install django. (after this I usually use ln -s to link specific python site-package folder to vendor folder under the same django project.)
If you’re using Ubuntu linux for your django project, try following commands to setup your django project:
sudo apt-get install python3-pip
sudo apt-get install python3-venv
python3 -m venv django-project/vendor
cd django-porject
source vendor/bin/activate
安装Django: pip install django 指定版本 pip3 install django==3.0
新建项目: django-admin.py startproject mysite
新建APP : python manage.py startapp blog
启动:python manage.py runserver 8080
同步或者更改生成 数据库:
python manage.py makemigrations
python manage.py migrate
清空数据库: python manage.py flush
创建管理员: python manage.py createsuperuser
修改用户密码: python manage.py changepassword username
Django项目环境终端: python manage.py shell
这个命令和 直接运行 python 进入 shell 的区别是:你可以在这个 shell 里面调用当前项目的 models.py 中的 API,对于操作数据的测试非常方便。
更多关于Django的命令在终端输入:python manage.py 查看
pip isntall -r requirements.txt
django settings.py setup:
ALLOWED_HOSTS = [your server ip address]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
run: python manage.py collectstatic
sudo apt-get install apache2
sudo apt-get install libapache2-mod-wsgi-py3
cd /etc/apache2/sites-available
configure your django vhost including aliases for your /static and /media folders, WSGI and python venv path.
And finally run
$sudo a2ensite django-project
$sudo a2dissite 000-default.conf
$sudo service apache2 restart
django-admin startproject django_weber
python manage.py help
python manage.py startproject project_name
python manage.py startapp app_name
python manage.py makemigration
python manage.py sqlmigrate app_name migration_file_name (e.g. ecommence 0001)
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
python manage.py shell
from blog.models import Post
from django.contrib.auth.models import User
users = User.objects.all()
user = User.objects.get(id=1)
user = User.objects.first()
user = User.objects.last()
users = User.objects.filter(username='hok')
user = User.objects.filter(username='hok').first()
user.id
user.pk
user.username
user.post_set.all()
user.post_set.create(title='tltle', content='content')
pip isntall django-crispy-forms
If you’re using vscode for your python development IDE, remember to set
"python.autoComplete.extraPaths": ["/data/wwwroot/python/django_weber"],
in your settings.json
file. This will help you to navigate through all your python modules.
Here’s Django official document to tell the difference between django-admin and manage.py: https://docs.djangoproject.com/en/3.0/ref/django-admin/
django-admin
and manage.py
¶
django-admin
is Django’s command-line utility for administrative tasks. This document outlines all it can do.
In addition, manage.py
is automatically created in each Django project. It does the same thing as django-admin
but also sets the DJANGO_SETTINGS_MODULE
environment variable so that it points to your project’s settings.py
file.
The django-admin
script should be on your system path if you installed Django via pip
. If it’s not on your path, you can find it in site-packages/django/bin
within your Python installation. Consider symlinking it from some place on your path, such as /usr/local/bin
.
For Windows users, who do not have symlinking functionality available, you can copy django-admin.exe
to a location on your existing path or edit the PATH
settings (under Settings - Control Panel - System - Advanced - Environment...
) to point to its installed location.
Generally, when working on a single Django project, it’s easier to use manage.py
than django-admin
. If you need to switch between multiple Django settings files, use django-admin
with DJANGO_SETTINGS_MODULE
or the --settings
command line option.
The command-line examples throughout this document use django-admin
to be consistent, but any example can use manage.py
or python -m django
just as well.