Django与Horizon( by quqi99 )

Django与Horizon( by quqi99 )

作者:张华  发表于:2013-09-11
版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明

( http://blog.csdn.net/quqi99)


很少接触前端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: [email protected]
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)


20150203更新,上述调试方法适用于django 1.4, 对于django 1.6不需要像上面一样修改manage.py, 直接在eclipse pydev以runserver --noreload运行horizon即可调试。如果遇到django抛VariableDoesNotExist异常(不以debug模式运行没事)时,可这样解决: 在弹出来的"Caught Exceptions"视图里点右键后点击“Ignore caught exceptions", 然后在debug视图上接着点击"PyDev > Manage Exception Breakpoints”菜单将“uncheck Suspend on django template render exception”前的勾去掉即可。

如果调试horizon auth, 在eclipse里,设置horizon工程依赖于django_openstack_auth工程,在backend.py里的authenticate方法设置断点了慢慢调吧。
如果在debug模式时登录失败,那是因为在sudo /etc/init.d/apache2 stop时把keystone-all也给停了,在eclipse里启动即可


$horizon/horizon/base.py中的HorizonSite类代表整个网站,它继承Site类。

Site类是一个容器类,包含dashboardpanel,及panel下相关url结构解析的相关方法。

概念:一个site下有多个dashboard(其中projectadmin是两个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开发指南

你可能感兴趣的:(Django与Horizon( by quqi99 ))