Code Life of gladuo - django
type: channel
link: https://tech.gladuo.com/tag/django/
items: Workflow for Django

Workflow for Django
type: item
date: 2016-02-09T17:37:00+00:00
description: Workflow for DjangoGithubadd / create / clone repoadd / commitsyncrevert虚拟环境mkvirtualenv project_namermvirtualenv project_namesetvirtualenvproject project_name PATHworkon project_namedeactiveStart Projectdjango-admin.py startproject project_namepython manage.py migratepython manage.py runserverStart App1. Start Apppython manage.py startapp app_name2. 修改 settings.py在project_name的settings.py文件中，找到INSTALLED_APPS列表，在列表的最后面增加 app_nameINSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app_name', ]3. 创建视图在app_name目录内的views.py文件中，删除注释# Create your views here.，创建视图def index(request): context_dict = {'boldmessage': "I am bold font from the context"} return render(request, 'rango/index.html', context_dict)4. 应用 URL 映射在app_name目录中，创建一个叫做urls.py的文件。from django.conf.urls import patterns, url from rango import views urlpatterns = [ url(r'^$', views.index, name='index')]5. 项目 URL 映射在project_name/project_name/urls.py中，更新urlpatterns列表。urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^app_name/', include('app_name.urls')) # for example ]使用模板1. 创建目录在project_name/创建一个templates目录，在这个目录中为每一个 app 建立一个目录，例如 project_name/templates/app_name，用来存放app_name应用下的模板2. 在 settings.py 中设置在setting.py中创建TEMPLATE_PATH变量，用它来储存 templates 目录。这里我们使用os.path.join()函数。TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')TEMPLATE_DIRS = [ # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". # Always use forward slashes, even on Windows. # Don't forget to use absolute paths, not relative paths. TEMPLATE_PATH, ]3. 添加模板在template/app_name/目录里建立一个叫做index.html的文件,在新文件里加入下面代码： App App says... hello world! {{ boldmessage }} About 4. 在 views.py 中 renderfrom django.shortcuts import render def index(request): # Construct a dictionary to pass to the template engine as its context. # Note the key boldmessage is the same as {{ boldmessage }} in the temp…