如何在 Django 和 Javascript 之间共享信息?
所以我对 Django 和 Javascript 都很陌生(我正在使用 JQuery),我想知道我是否在进行黑客攻击,或者是否有更巧妙的方法将客户端显示的数据库 id 发送到 django 服务器端。这是我的过程:
我有一个 dataTable (http://datatables.net),我使用它来显示数据行bProcessing 选项使用 AJAX 从数据库检索记录。我的 urls.py 中的 URL 类似于:
url(r'^assets/activitylog/(?P<cid>.*)$', views.getActivityTable_ajax, name="activitylog_table"),
我的 dataTable ajax 相关代码类似于:
"sAjaxSource": "/assets/activitylog/" + getIDFromHTML(),
其中 javascript 函数 getIDFromHTML()
抓取
,即Django 视图使用的内容很简单:
function getIDFromHTML(){
// Simply return the text in the #release_id div element from the HTML
return $("#release_id").html();
};
这对我来说似乎是“hacky”的部分。我将在数据表 URL 中使用的数据库 ID(在 CSS 中使用 display:none)插入到我的模板代码中,这样我就可以将其传递回视图。其中大部分是必要的,因为除非代码嵌入到 HTML 本身中,否则无法在 javascript 代码中使用 django 模板标签,而我不会(也不会)这样做。
我发现的唯一的另一件事是更改 URL 以删除传入的参数:
url(r'^assets/activitylog', views.getActivityTable_ajax, name="activitylog_table"),
并将视图代码更改为:
def getActivityTable_ajax(request):
"""Returns the activity for a given pid from HTTP GET ajax reqest"""
pid = int(urlparse.urlparse(request.META['HTTP_REFERER']).path.split('/')[-1])
# rest of view code here...
因为我需要的 id 位于此引用 URL 的末尾。
这样我就不必费力地将隐藏的数据库 ID 嵌入到 HTML 中并通过 ajax 将其传递回表填充视图代码。
以这种方式在请求对象中使用 HTTP_REFERER 可以吗?我是否以完全错误的方式处理这件事?
提前致谢!
So I am pretty new to both Django and Javascript (I am using JQuery) and I am wondering if I am doing a hack or if there are more slick ways to send client-side displayed database ids to the django server-side. Here is my process:
I have a dataTable (http://datatables.net) that I am displaying rows of data by using the bProcessing option to use AJAX to retrieve records from the database. The URL in my urls.py is something like:
url(r'^assets/activitylog/(?P<cid>.*)
and my dataTable ajax-relavant code is something like:
"sAjaxSource": "/assets/activitylog/" + getIDFromHTML(),
where the javascript function getIDFromHTML()
grabs <cid>
that is used by the Django view is simply:
function getIDFromHTML(){
// Simply return the text in the #release_id div element from the HTML
return $("#release_id").html();
};
This is the part that seems "hacky" to me. I am inserting into my template code the database id that I am using in the datatables URL (with display:none in the css) just so I can pass it back to the view. Most of this is necessitated because one cannot use django template tags in the javascript code unless the code is embedded into the HTML itself, which I am not (and will not) do.
The only other thing that I have found is to change the URL to get rid of the parameter passed in to:
url(r'^assets/activitylog', views.getActivityTable_ajax, name="activitylog_table"),
and change the view code to:
def getActivityTable_ajax(request):
"""Returns the activity for a given pid from HTTP GET ajax reqest"""
pid = int(urlparse.urlparse(request.META['HTTP_REFERER']).path.split('/')[-1])
# rest of view code here...
since the id that I need is on the end of this referer url.
This way I don't have to monkey around with embedding the hidden database id into the HTML and passing it back to via ajax the the table population view code.
Is it okay to use HTTP_REFERER in the request object in this manner? Am I going about this in the totally wrong way?
Thanks in advance!
, views.getActivityTable_ajax, name="activitylog_table"),
and my dataTable ajax-relavant code is something like:
where the javascript function getIDFromHTML()
grabs <cid>
that is used by the Django view is simply:
This is the part that seems "hacky" to me. I am inserting into my template code the database id that I am using in the datatables URL (with display:none in the css) just so I can pass it back to the view. Most of this is necessitated because one cannot use django template tags in the javascript code unless the code is embedded into the HTML itself, which I am not (and will not) do.
The only other thing that I have found is to change the URL to get rid of the parameter passed in to:
and change the view code to:
since the id that I need is on the end of this referer url.
This way I don't have to monkey around with embedding the hidden database id into the HTML and passing it back to via ajax the the table population view code.
Is it okay to use HTTP_REFERER in the request object in this manner? Am I going about this in the totally wrong way?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是不正确的。只需使
标记指向 urlconf 中的某些内容,然后查看视图 为 JavaScript 设置适当的
Content-Type
header 并输出。This is incorrect. Simply make the
<script>
tag point to something in your urlconf, then have the view set theContent-Type
header appropriately for JavaScript and output it.我上周刚刚使用 Django 设置了一些数据表。
唯一的要求是您的服务器发送一个名为 aaData 的 JSON 结构,其中包含行。
您还可以使用这个完整的示例作为基础,这是更高级的,并且有一些您必须在项目中进行 cipy 的方法(datatables.utils):
http://www.datatables.net/development/server-side/django
I just setup a few datatables last week with Django.
The only requirement is that your server sends a JSON structure called aaData, containing the rows.
You can also use this full example as a basis, this is more advanced and has a few methods you have to cipy in your projects (datatables.utils) :
http://www.datatables.net/development/server-side/django