Django回忆录
Posted ahaii
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django回忆录相关的知识,希望对你有一定的参考价值。
Django使用回忆:
1、安装django:
pip install django==1.9
2、创建项目及应用:
django-admin startproject web
django-admin startap blog
3、配置数据库、二级url:
web/setting.py: INSTALLED_APPS = [ ‘django.contrib.admin‘, ‘django.contrib.auth‘, ‘django.contrib.contenttypes‘, ‘django.contrib.sessions‘, ‘django.contrib.messages‘, ‘django.contrib.staticfiles‘, ‘blog‘, ] ... DATABASES = { ‘default‘: { # ‘ENGINE‘: ‘django.db.backends.sqlite3‘, # ‘NAME‘: os.path.join(BASE_DIR, ‘db.sqlite3‘), ‘ENGINE‘: ‘django.db.backends.mysql‘, ‘NAME‘:‘test_blog‘, ‘USER‘:‘ahaii‘, ‘PASSWORD‘:‘123456‘, ‘HOST‘:‘‘, ‘PORT‘:‘‘, } }
web/urls.py: urlpatterns = [ url(r‘^admin/‘, admin.site.urls), url(r‘^blog/‘, include(‘blog.urls‘)), ]
blog/urls.py: from blog import views urlpatterns = [ url(r‘^$‘, views.index), ]
blog/views.py: from django.shortcuts import render,HttpResponse # Create your views here. def index(request): return HttpResponse(‘Hello Django‘)
4、创建数据库模型:
blog/models.py: from django.db import models # Create your models here. class Article(models.Model): title = models.CharField(max_length=100) category = models.CharField(max_length=50,blank=True) date_time = models.DateTimeField(auto_now_add=True) content = models.TextField(blank=True,null=True) def __unicode__(self): return self.title class Meta: ordering = [‘-date_time‘]
5、同步数据库:
python manage.py migrate
python manage.py makemigrations
6、创建超级用户:
python manage.py createsuperuser
7、利用python shell交互模式操作数据库:
python manage.py shell >>>from blog.models import Article >>>Article.objects.create(title=‘...‘,category=‘...‘,content=‘...‘)//插入数据 >>>Article.objects.all()//查询所有记录,返回列表 >>>Article.objects.get(id=1)//查询第一条
>>>a=Article.objects.get(title=‘Car‘)
>>>a.content=‘The traffice is so busy!‘//修改内容
>>>>a.save()//写入数据库
8、动态url:(获取url中的参数)
blog/views.py:
def detail(request,args):
post = Article.objects.all()[int(args)] //默认输入为字符串,转换为整型,列表索引
str = (‘title=%s,category=%s,content=%s‘) %(post.title,post.category,post.content)
return HttpResponse(str)
blog/urls.py: urlpatterns = [ url(r‘^$‘, views.index), url(r‘(?P<args>\d+)/$‘,views.detail)// \w+可匹配数字和字母 ]
9、使用模版:
web项目下创建templates/test.html,设置模版路径:
web/setting: TEMPLATES = [ { ‘BACKEND‘: ‘django.template.backends.django.DjangoTemplates‘, ‘DIRS‘: [ os.path.join(BASE_DIR,‘templates‘) ], ‘APP_DIRS‘: True, ‘OPTIONS‘: { ‘context_processors‘: [ ‘django.template.context_processors.debug‘, ‘django.template.context_processors.request‘, ‘django.contrib.auth.context_processors.auth‘, ‘django.contrib.messages.context_processors.messages‘, ], }, }, ]
templates/test.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Just for test template</title> <style> body{ background-color: red; } strong{ color:lightseagreen; } </style> </head> <body> <h1>Hello Django</h1> <strong>{{ time }}</strong> </body> </html>
blog/views:
from datetime import datetime def template(request): return render(request,‘test.html‘,{‘time‘:datetime.now()})
blog/urls: urlpatterns = [ url(r‘template/$‘,views.template) ]
以上是关于Django回忆录的主要内容,如果未能解决你的问题,请参考以下文章
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段