使用django从外部数据库拉取数据到模板 [英] Pulling data to the template from an external database with django

查看:383
本文介绍了使用django从外部数据库拉取数据到模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将尝试构建一个网络应用程序,用户可以访问网址,登录和查看报告和其他信息。但是,报告的数据存储在外部数据库中。这是一个我将要访问的MySQL数据库。

I'm going to attempting to build a web app where users can visit a url, login and view reports and other information. However the data for the reports are stored in an external database. It's a MySQL database which I'm going to have access to.

我已经对谷歌做了一些研究,没有太多的运气找到任何例子。我已经做了一些阅读来连接到多个数据库 - https:/ /docs.djangoproject.com/en/dev/topics/db/multi-db/ 所以看起来我可以连接到数据库ok。

I've done a little research on google and not have much luck finding any examples. I've done a little reading into connecting to multiple databases - https://docs.djangoproject.com/en/dev/topics/db/multi-db/ So it looks i can connect to the database ok.

下一部分是我被卡住的地方。数据库中的数据将始终进行更新。我不想编辑信息,我也不想覆盖任何东西。我只是想能够连接到DB拉取所需的信息,然后通过模板查看用户,以便能够看到。首先是因为数据一直在更新,这是一个问题吗? (我希望不要!)

The next part is where I'm stuck. The data in the database is going to be updated all the time. I don't want to be able to edit the information neither do i want to be able to overwrite anything. I just want to be able to connect to the DB pull the information required and then view it through the template for user to be able to see. First of all because the data is being updated all the time, is this going to be a problem? (I hope not!)

一旦连接到数据库,最好能够拉出数据,然后将其放入我可以输出到模板吗?我需要将数据导入到模型中,然后用视图进行控制。或者我需要使用JSON或XML转换数据?

Once I've connected to the database, what is the best to be able to pull out the data and then put it into a format that i can output to the template? Would i need to import the data in to models, then control with the view. Or would i need to convert the data with JSON or XML?

我对python / django来说是相当新鲜的,所以任何帮助都将不胜感激。如果您需要更多信息,请提前询问并感谢。 :)

I'm fairly new to python / django, so any help would be much appreciated. If you need anymore info please ask and thanks in advance. :)

推荐答案

没问题!我一直这样做。

No problem! I do this all the time.

至于不编辑或更新数据,只需不要添加任何更新内容的应用程序数据。 Salem关于在MySQL方面使用权限的建议也是一个好主意。

As far as the "don't edit or update the data", just don't add anything to your app that would update the data. Salem's suggestion about using permissions on the MySQL side is a good idea as well.

为了检索数据,您有两个选择:

For retrieving the data, you have two options:

1)您可以创建与MySQL数据库中的表对应的Django模型。您可以手动执行此操作,也可以使用inspectdb命令与manage.py给您一个良好的起点。然后这样做:

1) You can create Django models that correspond to your tables in the MySQL database. You can do this manually, or you can use the "inspectdb" command with manage.py to give yourself a good starting point. Then do something like this:

def myview(request):
  rows = MyModel.objects.using('mysql').all()
  return render_to_response("mytemplate.html", {"rows" : rows })

2)您可以在应用程序中手动管理连接和查询。这在一个视图中是完全有效的:

2) You can manage the connections and queries manually within your app. This is perfectly valid within a view:

def myview(request):
  conn = MySQLdb.connect("connection info here")
  try:
    cursor = conn.cursor()
    cursor.execute("select * from mytable")
    rows = cursor.fetchall()
  finally:
    conn.close()

  return render_to_response("mytemplate.html", {"rows" : rows})

终于 - Django非常高兴使用MySQL作为数据库。如果您的DBA将让Django在同一个数据库中创建表,那么它可能会简化。

finally -- Django is perfectly happy to use MySQL as a database. It might simplify things if your DBA will let Django create its tables right in the same database.

这篇关于使用django从外部数据库拉取数据到模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆