django怎么链接linux数据库

django怎么链接linux数据库,第1张

编辑 新建的project 配置文件(settingspy):

[root@itchenyi-1 Django-133]# vi itchenyi/settingspy

DATABASES = {

'default': {

'ENGINE': 'djangodbbackendsmysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'

'NAME': 'itchenyi_db', # Or path to database file if using sqlite3

'USER': 'itchenyi', # Not used with sqlite3

'PASSWORD': 'your password', # Not used with sqlite3

'host': '', # set to empty string for localhost Not used with sqlite3

'PORT': '', # Set to empty string for default Not used with sqlite3

}

}

5、切换到新建的project 创建数据库和表:

[root@itchenyi-1 Django-133]# cd itchenyi/

[root@itchenyi-1 itchenyi]# python managepy 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 auth_message

Creating table django_content_type

Creating table django_session

Creating table django_site

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 'root'): itchenyi

E-mail address: itchenyi@gmailcom

Password:

Password (again):

Superuser created successfully

Installing custom SQL

Installing indexes

No fixtures found

6、简单验证:

[root@itchenyi-1 itchenyi]# python managepy Shell

Python 266 (r266:84292, Dec 7 2011, 20:48:22)

[gcc 446 20110731 (Red Hat 446-3)] on linux2

Type "help", "copyright", "credits" or "license" for more information

(InteractiveConsole)

>>> import MySQLdb

>>> db = MySQLdbconnect(user='itchenyi',db='itchenyi_db',passwd='your password'

,host='localhost')

>>>

首先,在django 视图函数中,传递 obj_list = [1, 2, 3] 类似这样的一个列表。

def show_data(request):

    obj_list = [1, 2, 3] 

    pass

    return render_to_response('indexhtml', {'obj_list': obj_list})

然后在 indexhtml 模板文件中,部分代码如下

<select>

    {% for obj in obj_list %}          

        <option value ="{{ forloopcounter }}">{{ obj }}</option>

    {% endfor %}

</select>

这样,模板就能自动根据传递过来的数据,进行显示。

这样说的已经很详细了,如果还是不懂。自己去django官网看相应版本的文档去吧。

settingspy正常,mysql的配置文件也改过了。问题出在没有指定读取的配置文件,

修改 aettingspy :

迁移成功。

之前使用redis时,也发生过类似问题,要手动指定配置文件。

以上就是关于django怎么链接linux数据库全部的内容,包括:django怎么链接linux数据库、django将从数据库获取的值作为下拉选择框(Choice_field)的默认值、django连接mysql数据库'connection refused'的解决等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9792931.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存