• python创建项目


    一、准备下载

    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 

    点击执行,然后在浏览器中输入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>

    三、开始创建数据库

    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:UsersadminAppDataLocalTemppip-record-2fiij4o6install-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

    
    
  • 相关阅读:
    我眼中的SCRUM
    文本转换程序
    免费接口
    看板,敏捷的另一种实现方式
    Android Asynchronous Http Client-Android异步网络请求客户端接口
    hdu4753 Fishhead’s Little Game 状态压缩,总和一定的博弈
    dbcp、c3p0、jdbc常用连接配置
    IE安全分析
    redis入侵小结
    heartbleed漏洞利用
  • 原文地址:https://www.cnblogs.com/GainLoss/p/9552688.html
Copyright © 2020-2023  润新知