免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1940 | 回复: 1
打印 上一主题 下一主题

django连接数据库问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-04-15 16:36 |只看该作者 |倒序浏览


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

请哥哥指定,哪里配置不当?

论坛徽章:
0
2 [报告]
发表于 2013-04-17 09:40 |只看该作者
问题可以通过以下解决:
1.首先要判断整个网站的数据库连接是否有问题,建表是否可行,连接是否可行
2.问题是在views.py上面的,好像引入语句from models import Employee 不起作用,看看是否有其他问题
3.如果在manage.py shell下面引入和调用正常的话,应该没什么问题。

最后建议使用python2.6,以前我也使用2.7,但有时候在使用django的时候,有莫名的问题出现,
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP