1、下载安装MySQLdb类库
http://www.djangoproject.com/r/python-mysql/
2、修改settings.py 配置数据属性
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'djangodb', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '3306', # Set to empty string for default.
}
}
修改完后进入DOS进入项目目录下执行python manage.py shell命令启动交互界面输入一下代码验证数据库配置是否成功。没报错则成功!
>>> from django.db import connection
>>> cursor = connection.cursor()
3、创建一个Django app
一个项目中包含一个或多个这样的app。app可以理解为一块功能集合。比如产品管理模块就包含增删该查等功能,可以把产品管理叫做一个app。每个Django app都有独立的models,views等,易移植和被复用。
DOS进入项目目录 执行 python manage.py startapp products生成目录文件如下:
products/
__init__.py
models.py
tests.py
views.py
4、编写models
from django.db import models
# Create your models here.
class Company(models.Model):
full_name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
tel = models.CharField(max_length=15,blank=True)
class Product(models.Model):
product_name = models.CharField(max_length=30)
price = models.FloatField()
stock = models.IntegerField(max_length=5)
company = models.ForeignKey(Company)
5、模型安装(修改settings.py)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
'django.contrib.admindocs',
'DjangoMysqlSite.products',
)
采用 python manage.py validate 检查模型的语法和逻辑是否正确。
没有错误则执行 python manage.py syncdb创建数据表。
现在你可以看到你的数据库除了生成了products_company,products_product外还创建了其它好几个表,这些是django管理后台所需表暂不管。
6、简单的增删改查
进入python manage.py shell
from DjangoMysqlSite.products.models import Company
>>> c = Company(full_name='集团',address='杭州西湖',tel=8889989)
>>> c.save()
>>> company_list = Company.objects.all()
>>> company_list
>>> c = Company.objects.get(full_name="集团")
>>> c.tel = 123456
>>> c.save()
>>> c = Company.objects.get(full_name="集团")
>>> c.delete()
#删除所有
>>> Company.objects.all().delete()

InPython,youappendelementstoalistusingtheappend()method.1)Useappend()forsingleelements:my_list.append(4).2)Useextend()or =formultipleelements:my_list.extend(another_list)ormy_list =[4,5,6].3)Useinsert()forspecificpositions:my_list.insert(1,5).Beaware

The methods to debug the shebang problem include: 1. Check the shebang line to make sure it is the first line of the script and there are no prefixed spaces; 2. Verify whether the interpreter path is correct; 3. Call the interpreter directly to run the script to isolate the shebang problem; 4. Use strace or trusts to track the system calls; 5. Check the impact of environment variables on shebang.

Pythonlistscanbemanipulatedusingseveralmethodstoremoveelements:1)Theremove()methodremovesthefirstoccurrenceofaspecifiedvalue.2)Thepop()methodremovesandreturnsanelementatagivenindex.3)Thedelstatementcanremoveanitemorslicebyindex.4)Listcomprehensionscr

Pythonlistscanstoreanydatatype,includingintegers,strings,floats,booleans,otherlists,anddictionaries.Thisversatilityallowsformixed-typelists,whichcanbemanagedeffectivelyusingtypechecks,typehints,andspecializedlibrarieslikenumpyforperformance.Documenti

Pythonlistssupportnumerousoperations:1)Addingelementswithappend(),extend(),andinsert().2)Removingitemsusingremove(),pop(),andclear().3)Accessingandmodifyingwithindexingandslicing.4)Searchingandsortingwithindex(),sort(),andreverse().5)Advancedoperatio

Create multi-dimensional arrays with NumPy can be achieved through the following steps: 1) Use the numpy.array() function to create an array, such as np.array([[1,2,3],[4,5,6]]) to create a 2D array; 2) Use np.zeros(), np.ones(), np.random.random() and other functions to create an array filled with specific values; 3) Understand the shape and size properties of the array to ensure that the length of the sub-array is consistent and avoid errors; 4) Use the np.reshape() function to change the shape of the array; 5) Pay attention to memory usage to ensure that the code is clear and efficient.

BroadcastinginNumPyisamethodtoperformoperationsonarraysofdifferentshapesbyautomaticallyaligningthem.Itsimplifiescode,enhancesreadability,andboostsperformance.Here'showitworks:1)Smallerarraysarepaddedwithonestomatchdimensions.2)Compatibledimensionsare

ForPythondatastorage,chooselistsforflexibilitywithmixeddatatypes,array.arrayformemory-efficienthomogeneousnumericaldata,andNumPyarraysforadvancednumericalcomputing.Listsareversatilebutlessefficientforlargenumericaldatasets;array.arrayoffersamiddlegro


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

WebStorm Mac version
Useful JavaScript development tools
