问题导读
问题1:如何创建并且运行一个简易Blog?
问题2:如何在eclipse中调试horizon?
无论你对前端web开发有多少了解,想看看horizon的结构,那么请先花一点点时间稍微了解一下Django。
创建工程
django-admin startproject mysite
启动应用服务器
cd mysite && python manage.py runserver
创建应用
./manage.py startapp blog ,
编辑mysite/setting.py文件在INSTALLED_APPS中添加'blog',
数据库Model
[hua@laptop mysite]$ cat blog/models.py
from django.db import models
class BlogPost(models.Model):
title = models.CharField(max_length=150)
body = models.TextField()
timestamp = models.DateTimeField()
数据库配置,mysite/setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'djangodb',
'USER': 'root',
'PASSWORD': 'password',
'HOST': '172.16.1.122',
'PORT': '3306',
}
}
创建库之后再创建表
[hua@laptop mysite]$ vi mysite/settings.py
[hua@laptop mysite]$ ./manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table blog_blogpost
You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'hua'):
E-mail address: abcdefg@123.com
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
设置自动admin应用,即为表增加CRUD方法, 在mysite/setting.py的INSTALLED_APPS中添加:'django.contrib.admin',
再执行./manage.py syncdb命令后增加了数据表django_admin_log.
接着编辑mysite/urls.py文件,取消下列行前的注释:
# from django.contrib import admin
# admin.autodiscover()
# url(r'^admin/', include(admin.site.urls))
最后,在blog.models.py的最后添加一行admin.site.register(BlogPost),变成:
[hua@laptop mysite]$ cat blog/models.py
from django.db import models
from django.contrib import admin
class Author(models.Model):
name = models.CharField(max_length=150)
class BlogPost(models.Model):
title = models.CharField(max_length=150)
author = models.ForeignKey("Author", related_name="posts")
#authors = models.ManyToManyField("Author", related_name="posts")
body = models.TextField()
timestamp = models.DateTimeField()
class BlogPostAdmin(admin.ModelAdmin):
list_display = ('title', 'timestamp')
admin.site.register(Author)
admin.site.register(BlogPost, BlogPostAdmin)
上面已经演示了一对多和无其他字段的多对多的关系,如果是多对多的话可能还有一张关联表,并且关联表还有除两个外键以外的其他字段的话:
authors = models.ManyToManyField("Author", through="Authoring")
class Authoring(models.Model):
collaboration_type = models.CharFiled(max_length=100)
post = modles.ForeignKey(BlogPost)
author = models.ForeignKey(Author)
这时候就可以通过http://127.0.0.1:8000/admin/来访问编辑数据表了,用户名和密码是上步设置的。
创建模板与模板继承
[hua@laptop mysite]$ cat blog/templates/base.html
<h1> base template </h1>
{% block content %}
{% endblock %}
[hua@laptop mysite]$ cat blog/templates/archive.html
{% extends "base.html" %}
{% block content %}
{% for post in posts %}
<h2> {{ post.title }}</h2>
<p> {{ post.timestamp|date:"1, F jS" }} </p>
<p> {{ post.body }} </p>
{% endfor %}
{% endblock %}
创建视图
[hua@laptop mysite]$ cat blog/views.py
from django.template import loader, Context
from django.http import HttpResponse
from blog.models import BlogPost
def archive(request):
#posts = BlogPost.objects.all()
post = BlogPost.objects.get(title='blog1')
author = post.author
posts = author.posts.all()
t = loader.get_template("archive.html")
c = Context({'posts': posts })
return HttpResponse(t.render(c))
添加urls
一是mysite/urls.py中添加:url(r'^blog/', include('blog.urls'))
二是添加blog/urls.py文件
[hua@laptop mysite]$ cat blog/urls.py
from django.conf.urls.defaults import *
from blog.views import archive
urlpatterns = patterns('',
url(r'^$', archive),
)
这时候通过http://127.0.0.1:8000/blog/就可以访问了。
如果想要在eclipse使用pvdev调试horizon的是一件非常容易简单的事情,首先打上下列patch, 接着在eclipse中创建pydev工程,再在eclipse以debug模式运行python manage.py runserver--noreload即可,和nova, neutron其他工程用pydev调试的方法一模一样,除了下列patch,再没有任何特殊之处,所以没什么好说的。
[hua@laptop horizon]$ git diff manage.py
diff --git a/manage.py b/manage.py
index 256981c..738e9a5 100755
--- a/manage.py
+++ b/manage.py
@@ -2,10 +2,13 @@
import os
import sys
+from django.core.management import execute_manager
-from django.core.management import execute_from_command_line
+ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
+if ROOT_PATH not in sys.path:
+ sys.path.insert(0,ROOT_PATH)
+import openstack_dashboard.settings
if __name__ == "__main__":
- os.environ.setdefault("DJANGO_SETTINGS_MODULE",
- "openstack_dashboard.settings")
- execute_from_command_line(sys.argv)
+ os.environ['DJANGO_SETTINGS_MODULE'] = 'openstack_dashboard.settings'
+ execute_manager(openstack_dashboard.settings)
$horizon/horizon/base.py中的HorizonSite类代表整个网站,它继承Site类。
Site类是一个容器类,包含dashboard,panel,及panel下相关url结构解析的相关方法。
概念:一个site下有多个dashboard(其中project和admin是两个dashboard),一个dashboard下有多个panelgroup,一个panel group下有多个panel(如SystemPanel)。
View也是继承关系的,如:ModalFormView,MultiTableView, TabView
$horizon/horizon/templatetags/horizon.py会根据这些dashboard,view等构建网站。
也可以在$horizon/openstack_dashboard/setting.py的MIDDLEWARE_CLASSES定义一些中间件(一般具有process_request, process_view,process_exception, process_response等方法),如horizon.mddleware.HorizonMiddeware, 它的作用相当于拦截器, django会内省这些类,并按顺序调用它们。
代码流程:
1,manage.py中有”import openstack_dashboard.settings”, 它又会调用”from local.local_settingsimport*”,它里面的“from horizon.utils import secret_key”会触发加载$horizon/horizon/__init__.py,它会导入Dashboard,Horizon, Panel, PanelGroup这些类进入正题。
参考:
1,.Django Web开发指南
2.用Eclipse调试Horizon提高开发效率
|
|