- 论坛徽章:
- 0
|
1,setting.py
# Django settings for csvt03 project.
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
# ('Your Name', 'your_email@example.com'),
)
MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'csvt', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'root',
'PASSWORD': 'admin',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
2,urls.py
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'csvt03.views.home', name='home'),
# url(r'^csvt03/', include('csvt03.foo.urls')),
# 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)),
url(r'^index/$', 'blog.views.index'),
)
3,/blog/models.py
from django.db import models
# Create your models here.
class Employee(models.Model):
name = models.CharField(max_length=20)
def __unicode__(self):
return self.name
4,/blog/views.py
# Create your views here.
from django.shortcuts import render_to_response
from models import Employee
def index(req):
emps = Employee.objects.all()
return render_to_response('index.html',{'emps':emps})
5,csvt03\blog\templates\index.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
</head>
<body>
{{emps}}
123
</body>
</html>
-------使用ipython测试 OK-------------------
In [1]: from django.db import models
In [2]: emps = Employee.objects.all()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
D:\Python27\lib\site-packages\django\core\management\commands\shell.pyc in <modu
le>()
----> 1 emps = Employee.objects.all()
NameError: name 'Employee' is not defined
In [3]: from blog.models import Employee
In [4]: emps = Employee.objects.all()
In [5]: emps
Out[5]: [<Employee: Allen>, <Employee: tom>, <Employee: max>, <Employee: bill>]
Python manage.py runserver,
通过浏览器访问http://127.0.0.1:8000/index 无法接解析到emps
请哥哥指定,哪里配置不当? |
|