python virtual env
python3.6 -m venv test
source /bin/activate
install pip tools
pip install django==1.11.9
list pip installed package
pip list
Generate output suitable for a requirements file.
$ pip freeze > requirements.txt
install from it in another environment.
pip install -r requirements.txt
Django create project
django-admin startproject yourprojectname
Django create App
python manage.py startapp yourappname
Views
Create hello_django.html in ./templates folder
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>
in yourappname/views.py
from django.shortcuts import render
# Create your views here.
def hello_view(request):
return render(request, 'hello_django.html', {
'data': "Hello Django ",
})
URLconf
in urls.py
from django.conf.urls import url
from django.contrib import admin
from musics.views import hello_view
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^hello/', hello_view),
]
Models
in models.py
from django.db import models
# Create your models here.
class Music(models.Model):
song = models.TextField(default="song")
singer = models.TextField(default="AKB48")
last_modify_date = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = "music"
Migrations
python3.6 manage.py makemigrations
python3.3 manage.py migrate
Admin Site
in settings.py
INSTALLED_APPS = [
'django.contrib.admin',
......
]
urls.py
urlpatterns = [
url(r'^admin/',admin.site.urls),
.... ,
]
createsuperuser
python manage.py createsuperuser
./yourappname/admin.py
from django.contrib import admin
# Register your models here.
from django.contrib import admin
from musics.models import Music
admin.site.register(Music)
done
http://127.0.0.1:8000/admin
run server at anywhere to port 8000
./manage.py runserver 0.0.0.0:8000
To kill it with -9 you will
have to list all running manage.py processes, for instance:
ps aux | grep -i manage
kill -9 pid
Common Error
AllowHost Error
DisallowedHost at /
Invalid HTTP_HOST header: '192.168.0.102:8000'. You may need to add '192.168.0.102' to ALLOWED_HOSTS.
solve configure the settings.py files
ALLOWED_HOST = ['*']
# or
ALLOWED_HOST = ['xxx.xxx.xxx.xxx'] # your ip addresss
Django error,TemplateDoesNotExist at /hello
# = [os.path.join(BASE_DIR,"templates")]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,"templates")],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
django mysql solved problem
if python 2.x version If pip install mysqlclient produces an error and you use Ubuntu, try:
sudo apt-get install -y python-dev libmysqlclient-dev
sudo pip install mysqlclient
if your python version is 3.5, do a :
apt-get install python-dev
pip install mysqlclient
or
apt-get install python3.5-dev
pip3.5 install mysqlclient
if dont work try below
Note this is not tested for python 3.x
pip install wheel
pip install pymysql
in settings.py
import pymysql
pymysql.install_as_MySQLdb();
INITIAL DJANGO MIGRATIONS FOR EXISTING DB SCHEMA.
Step1: Empty the django_migrations table:
delete from django_migrations;
Step2: Remove all the files in migrations folders in each and every app of your project.
rm -rf <app>/migrations/
Step3: Reset the migrations for the “built-in” apps:
python manage.py migrate --fake
Step4: Create initial migrations for each and every app:
python manage.py makemigrations <app>.
Step5: Final step is to create fake initial migrations:
python manage.py migrate --fake-initial
Create model from existiong mysql database
果開發到一半才將資料庫轉成 MySQL,或是在 Django 中想要使用 MySQL 中的資料表,就必須在 Model 中寫好資料格式。但是已經建好表,要一個一個寫進 Model 起不是很麻煩?好在 Django 有個貼心功能,使用 inspectdb 就能把資料格式都匯入 Model囉! 顯示轉換後的 Model 內容.
python manage.py inspectdb > <yourapp>/models.py
python manage.py makemigrations <yourapp>
python manage.py migrate
if 如果啓動項目時報錯:
django.db.utils.ProgrammingError: (1146, “Table ‘vd.django_content_type’ doesn’t exist”)
把原表格中django_migrations表刪除資料之後,重新執行命令
Python manage.py makemigrations <yourapp>
Python manage.py migrate
然後再啓動項目就可以啦
python static file
in settings.py
... ...
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
python manage.py collectstatic