您的位置:首页 > 编程语言 > Python开发

Django1.8+Python3.4+Mysql5.5

2015-06-17 19:30 956 查看


1、开发环境

系统:





数据库:





编程语言:





框架:





IDE:

PyCharm 4.5


2、新建Django工程,配置

新建Django工程:

?
目录结构:





官网介绍:

The outer Django_Test/ root
directory is just a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.

manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin
and manage.py.

The inner Django_Test/ directory
is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it .

Django_Test/__init__.py:
An empty file that tells Python that this directory should be considered a Python package. (Read more
about packages in the official Python docs if you’re a Python beginner.)

Django_Test/settings.py:
Settings/configuration for this Django project. Django settings will tell
you all about how settings work.

Django_Test/urls.py:
The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in URL
dispatcher.

Django_Test/wsgi.py:
An entry-point for WSGI-compatible web servers to serve your project. See How
to deploy with WSGI for more details.

启动工程:

?
输入:http://localhost:8000





说明你已经正确启动了Django工程!

配置Django对Mysql操作:

关于python3.4连接mysql请看这篇文章:/article/3479395.html

默认情况下,Django连接的是SqlLite,现在我们需要配置Mysql:

现在我们用PyMySQL: 官网地址:https://github.com/PyMySQL/PyMySQL/

?
配置Django工程:

1.在__init__.py文件中,添加这句话

?
2.修改settings.py文件

?


3、自动生成数据库表

Django项目是按照模块来开发的,我们先建立一个.

?
结构:





user/migrations: 数据库迁移用文件夹,创建的migrate会存在这个文件夹里。

user/_init_.py: 初始化数据信息。

user/admin.py:当前应用在Django管理界面的设置。

user/models.py:模型文件,用 Python 类来描述数据表。

user.tests.py:单元测试文件。

user.views.py:视图展示部分。

配置:

user/models.py(表结构从网上资源活取得到,实际使用根据具体情况):

?
settings.py:

?
运行:

检查配置文件





生成数据库表:





数据库表:





有了这些,我们就可以在此基础堆积代码了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: