python建立項目

1、準備下載html

python3.6.6 https://www.python.org/downloads/windows/(須要注意你的電腦是32位仍是64位)python

mysql 5.1.72 https://dev.mysql.com/downloads/mysql/mysql

pip 18.0 https://pypi.org/project/pip/jquery

Django 1.11.7  https://www.djangoproject.com/download/ 或者用pip install django=18.0(注意若是是2.0的話,會出錯的) git

pymysql  0.9.0  pip3 install PyMySQLweb

軟件pycharm http://www.jetbrains.com/pycharm/sql

2、建立項目數據庫

1.開始建立項目django

文件----新項目---django windows

點擊執行,而後在瀏覽器中輸入127.0.0.1:8000能夠看到html頁面

項目:pythonweb_demo app名稱:pythonweb

2.項目中文件代碼

python_demo/python_demo/seeting.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'pythonweb' //添加你的app的名字
]
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',//採用mysql
        'NAME': 'appdata',//你的
        'USER': 'root',//登陸庫用戶名
        'PASSWORD': '123456',//密碼
        'HOST': '',//主機
        'PORT': '3306',//端口 注意用這個端口號,注意拼寫 其餘的應該沒問題
        'OPTIONS': {'isolation_level': None}//連接數據庫須要填寫這個
    },
} 
STATIC_URL = '/static/' 
STATICFILES_DIRS = [
    (
        os.path.join(BASE_DIR, "static")
    )
]

python_demo/python_demo/urls.py

from django.contrib import admin
from pythonweb import views
from django.conf.urls import url

urlpatterns = [
    url('admin/', admin.site.urls),
    url('views/', views.index)
]

python_demo/pythonweb/models.py

from django.db import models
# -*- coding: utf-8 -*-
# Create your models here.
class Mobile(models.Model):
    brand = models.IntegerField()
    size = models.FloatField()
    price = models.IntegerField()
    # age = models.IntegerField()
    print(brand)
    def __unicode__(self):
        # 在Python3中使用 def __str__(self)
        return self

python_demo/pythonweb/views.py

from django.shortcuts import render
from pythonweb.models import Mobile

# Create your views here.
def index(request):
    print(1)
    str = Mobile.objects.all()
    return render(request, 'index.html', {'str': str})

python_demo/templates/index.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>後臺主頁面</title>
</head>
<body>
<div class="home">
    姓名爲name1的年齡爲:{{ str.price }}
    {% for str in str %}
        <p>{{str.brand}}&nbsp&nbsp&nbsp:&nbsp&nbsp&nbsp{{str.price}}</p>
        <br>
    {% endfor %}
    <h3>這是home頁面</h3>
</div>
<script src="/static/jqueryHome.js"></script>
</body>
</html>

3、開始建立數據庫

1.開始連接數據庫,在cmd中打開python,執行命令

python manage.py makemigrations pythonweb

python manage.py migrate pythonweb

這樣就能夠在數據庫中創modle.py中對應表

2.在cmd中打開mysql,查看錶是否建立成功

mysql>show databases;//查看數據庫

mysql>use appdata;//進入數據庫

mysql>show tables;//嶄新當前數據庫中表

mysql>desc mobile;//展現表中的參數

mysql> SELECT * FROM appdata.pythonweb_mobile m;//展現表中數據 mobile是表 python_web是你的app的名字 自動添加的

mysql> insert into pythonweb_mobile(brand,size,price) values;//添加數據

mysql>SELECT * FROM appdata.pythonweb_mobile m;//展現數據是否添加成功

3.在pyCharm執行

4.在瀏覽器中打開127.0.0.1

4、資料

1.https://blog.csdn.net/gitzliu/article/details/54627517

https://www.2cto.com/database/201806/752142.html

https://blog.csdn.net/pugongying1988/article/details/72870264 field

5、錯誤:

1.Your STATICFILES_DIRS setting is not a tuple or list; "

ImproperlyConfigured: Your STATICFILES_DIRS setting is not a tuple or list; perhaps you forgot a trailing comma?

解決方案:

找到settings.py文件,

把STATICFILES_DIRS=(os.path.join(BASE_DIR,'static'))

改成STATICFILES_DIRS=[(os.path.join(BASE_DIR,'static'))]

2.安裝mysqlclient出現錯誤: install --record C:\Users\admin\AppData\Local\Temp\pip-record-2fiij4o6\install-record.txt --single-version-externally-managed --compile" failed with error 

python==3.6

 3.django鏈接mysql

https://blog.csdn.net/liuweiyuxiang/article/details/71101910

4.錯誤django.db.utils.ProgrammingError: (1064"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED' at line 1")

解決:setting.py--->DATABASES 添加  'OPTIONS':{'isolation_level':None}

5.運行錯誤TypeError: context must be a dict rather than set.

https://blog.csdn.net/zoulonglong/article/details/79611562

相關文章
相關標籤/搜索