我使用 Django 创建了一个非常简单的示例代码,但无法让模型值显示在我的页面上:
----------------------------- 主页/models.py
from django.db import models
class Home(models.Model):
msg = models.CharField(max_length=100)
@classmethod
def create(cls, msg):
home = cls(msg=msg)
# do something with the book
return home
home = Home.create("Hello World!")
------------------------------------home/views.py
from django.views.generic import TemplateView
from project.models import Home
class IndexView(TemplateView):
model = Home
template_name = 'home/index.html'
------------------------------------------ 模板/home/index.html
{{ home.msg }}
this is a test page. I am expecting to see this....
------------------------------------------------------- urls.py
from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
from django.views.generic import TemplateView
admin.autodiscover()
urlpatterns = patterns('',
# Home pagetentacl.urls
url(r'^$', TemplateView.as_view(template_name='home/index.html')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
-------------------------------------- 浏览器上的结果页面:
这是一个测试页面。我期待看到这个......
对于我的示例,我不想拥有数据库访问权限。我希望我的模型返回“hello world”字符串。index.html 上的 home.msg 不返回任何内容。这里缺少什么?