---
# System prepended metadata

title: Django 從零到有
tags: [web, python]

---

# Django 從零到有

###### tags:`web` `python`
---
參考: [vscode_django_note](https://github.com/twtrubiks/vscode_django_note) ， [建置第一個專案](https://ivanjo39191.pixnet.net/blog/post/139835277-python-django-%E5%AD%B8%E7%BF%92%E7%B4%80%E9%8C%84%28%E4%BA%8C%29-%E5%BB%BA%E7%AB%8B%E5%B0%88%E6%A1%88)


建置虛擬環境

我使用的是anaconda，先開啟anaconda prompt
```
conda env list   #看目前虛擬環境列表
conda create --name myenv python=3.7  #建立一個myenv環境版本是3.7
activate myenv   #啟動環境myenv 

#啟動之後 安裝 django

pip install django

django-admin startproject 專案名稱

專案名稱會創建出一個資料夾
```
![](https://i.imgur.com/TBP1Gv4.png)

---

![](https://i.imgur.com/5cLz7z6.png)

----

Ubuntu

https://chenuin.blogspot.com/2019/01/django-ubuntuapache2modwsgi-django.html
env:
啟動:
source env/bin/activate


Apach2.0 
啟動： 
sudo systemctl enable apache2
關閉：
sudo systemctl stop apache2
or 
sudo service apache2 stop
Apach error log 
/var/log/apache2/error.log
tail /var/log/apache2/error.log


html 要放在 templates 下，然後要在 settings.py
TEMPLATES 下面的DIRS 要記得給他路徑 
如
'DIRS': [os.path.join(BASE_DIR, 'templates')] or 'DIRS': [TEMPLATE_PATH]
[TEMPLATE_PATH]為自定義

error:
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
解：
wsgi file add 
import django
django.setup()

### ubuntu wsgi file:
#coding =utf-8
import os
import sys
import site



os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
path = '/var/www/web_project'
env_path = '/var/www/web_project/env/lib/python3.5/site-packages'
if path not in sys.path:
    sys.path.append(path)

if env_path  not in sys.path:
    sys.path.append(env_path)


site.addsitedir('/usr/local/lib/python3.5/dist-packages')

import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "UserAdmin.settings")
django.setup()

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()


### 新手使用 網站

https://djangogirlstaipei.gitbooks.io/django-girls-taipei-tutorial/content/django/models.html