http://www.runoob.com/django/django-model.html
django-admin startapp TestModel
/models.py
from django.db import models
class Test(models.Model):
name = models.CharField(max_length=20)
settings.py
$ python manage.py makemigrations TestModel # 让 Django 知道我们在我们的模型有一些变更
$ python manage.py migrate TestModel # 创建表结构
##############################1105
https://code.ziqiangxuetang.com/django/django-template2.html
python manage.py startapp learn
urls.py
url(r'^learn', include('learn.urls')),
settings.py
home.html
<!DOCTYPE html>
<html>
<head>
<title> 模板中获取相应的URL - 自强学堂</title>
</head>
<body>
<div>
计算 3 + 4:<br>
1. 用<span style="color:red;">{<span>% url 'add' %</span>}?a=3&b=4</span> 可以获取到链接:
<a href="{% url 'add' %}?a=3&b=4">add</a><br>
2. 用<span style="color:red;">{<span>% url 'add' 3 4 %</span>}</span> 可以获取到链接:
<a href="{% url 'add2' 3 4 %}">add2</a><br>
<pre>
# 看一下 urls.py 中的每一条对应的 name
# urls.py
urlpatterns = patterns('',
# Examples:
url(r'^add/$', 'calc.views.add', name='add'),
url(r'^add/(d+)/(d+)/$', 'calc.views.add2', name='add2'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
</pre>
</div>
</body>
</html>
##views
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return render(request, 'home.html')
def add(request):
a = request.GET['a']
b = request.GET['b']
c = int(a) + int(b)
return HttpResponse(str(c))
def add2(request, a, b):
c = int(a) + int(b
return HttpResponse(str(c))
####urls
from django.contrib import admin
from django.urls import path
from . import views
from django.conf.urls import url,include
urlpatterns = [
url(r'^$', views.home),
## url(r'^add/(d+)/(d+)/$', views.add, name='add'),
url(r'^add/$', views.add, name='add'),
url(r'^add/(d+)/(d+)/$', views.add2, name='add2'),
]
########1106 监控配置
http://www.cnblogs.com/Cherry-Linux/p/9555812.html
http://blog.51cto.com/haohaozhang/1702518
########1107
https://blog.csdn.net/u013802160/article/details/52240289
https://code.ziqiangxuetang.com/django/django-models.html
pip install D: empmysqlclient-1.3.13-cp36-cp36m-win32.whl
building '_mysql' extension
error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
https://blog.csdn.net/a475952074/article/details/79530009
https://www.lfd.uci.edu/~gohlke/pythonlibs/ for windows
pip install D: emp
solution
进入该网址http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted
下载twisted对应版本的whl文件Twisted‑17.5.0‑cp36‑cp36m‑win_amd64.whl
,cp后面是python版本,amd64代表64位,
运行 pip install C:UsersCRDownloadsTwisted-17.5.0-cp36-cp36m-win_amd64.whl
重新输入pip install scrapy即可
同理可解决wordcloud的报错
grant all on people.* to people identified by 'people'
-new project vitur_env learn_models
-pip install django
django-admin startproject learn_models
cd learn_models
django-admin.py startapp people
#
models.py
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=30)
age = models.IntegerField()
#
python manage.py makemigrations
python manage.py migrate
###models.py
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class Author(models.Model):
name = models.CharField(max_length=50)
qq = models.CharField(max_length=10)
addr = models.TextField()
email = models.EmailField()
def __str__(self):
return self.name
@python_2_unicode_compatible
class Article(models.Model):
title = models.CharField(max_length=50)
author = models.ForeignKey(Author)
content = models.TextField()
score = models.IntegerField() # 文章的打分
tags = models.ManyToManyField('Tag')
def __str__(self):
return self.title
@python_2_unicode_compatible
class Tag(models.Model):
name = models.CharField(max_length=50)
def __str__(self):
return self.name
#######debug:
$python manage.py shell
from blog.models import Article, Author, Tag
from django.db.models import Sum
from blog.models import *
str(Author.objects.all().query)
str(Author.objects.filter(name="WeizhongTu").query)
Article.objects.filter(author__name='zhe').values_list('title', flat=True)
Tag.objects.all().extra(select={'tag_name': 'name'}).query.__str__()
Tag.objects.all().extra(select={'tag_name': 'name'}).defer('name').query.__str__()
Article.objects.all().values('author_id').annotate(count=Count('author')).values('author_id', 'count').query.__str__()
Article.objects.all().values('author__name').annotate(count=Count('author')).values('author__name', 'count').query.__str__()
articles = Article.objects.all().select_related('author')[:10]
articles = Article.objects.all().prefetch_related('tags')[:10]
articles = Article.objects.all()[:3]
for a in articles:
...: a.title, a.tags.all()
$ python manage.py dbshell
SQLite version 3.14.0 2016-07-26 15:17:14
Enter ".help" for usage hints.
sqlite> select name from blog_author limit 1;
############
>>>from django.db import models
>>> from blog.models import Article1
>>> from blog.fields import ListField
>>> a = Article1()
a.labels
>>> a.labels.append('Django')
>>> a.labels.append('custom fields')
>>> a.labels
['Django', 'custom fields']
>>> type(a.labels)
<type 'list'>
>>> a.content = u'我正在写一篇关于自定义Django Fields的教程'
>>> a.save()
###############test.py
from django.db import models
import ast
class ListField(models.TextField):
### __metaclass__ = models.SubfieldBase
#### description = "Stores a python list"
def __init__(self, *args, **kwargs):
super(ListField, self).__init__(*args, **kwargs)
def to_python(self, value):
if not value:
value = []
if isinstance(value, list):
return value
return ast.literal_eval(value)
def get_prep_value(self, value):
if value is None:
return value
return str(value)
def value_to_string(self, obj):
value = self._get_val_from_obj(obj)
return self.get_db_prep_value(value)
class Dummy(models.Model):
mylist = ListField()
>>> from blog.test import Dummy, ListField
>>> d = Dummy()
>>> d.mylist
''
>>> d.mylist = [3,4,5,6,7,8]
>>> d.mylist
[3, 4, 5, 6, 7, 8]
>>> f = ListField()
>>> f.get_prep_value(d.mylist)
'[3, 4, 5, 6, 7, 8]'
type(d.mylist)
<class 'list'>
########
##debug
settings.py 尾部加上
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django.db.backends': {
'handlers': ['console'],
'level': 'DEBUG' if DEBUG else 'INFO',
},
},
}
##########3
show table status from class_7 like 'test_info';
##########
https://zhidao.baidu.com/question/1435716091315804459.html
append方法是列表(list)方法,你定义的类型是一个字符串(str),字符串没有append方法。字符串添加元素和列表添加元素
上面的a是字符串,b是列表
#######