首页 技术 正文
技术 2022年11月6日
0 收藏 485 点赞 780 浏览 3593 个字

一、准备下载

python3.6.6 https://www.python.org/downloads/windows/(需要注意你的电脑是32位还是64位)

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

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

Django 1.11.7  https://www.djangoproject.com/download/ 或者用pip install django=18.0(注意如果是2.0的话,会出错的)

pymysql  0.9.0  pip3 install PyMySQL

软件pycharm http://www.jetbrains.com/pycharm/

二、创建项目

1.开始创建项目

文件—-新项目—django

python创建项目点击执行,然后在浏览器中输入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 urlurlpatterns = [
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>

三、开始创建数据库

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

四、资料

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

五、错误:

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

python创建项目

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


    					
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,111
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922