AJAX与Django(或任何Web框架)的“最佳实践”是什么?

时间:2022-07-03 20:04:21

I am developing an issue tracking application in Django, mostly for a learning exercise but also for my own projects - and I am looking into using some AJAX for "enhanced" usability. For example, allowing users to "star" particular issues, which would add them to their watch list. This is implemented in a lot of sites, and is often AJAX - as the URL that the user is viewing doesn't need to change when they click the star.

我正在Django开发一个问题跟踪应用程序,主要用于学习练习,也适用于我自己的项目 - 我正在研究使用一些AJAX来增强“可用性”。例如,允许用户“加注”特定问题,这会将其添加到监视列表中。这是在很多站点中实现的,并且通常是AJAX - 因为用户正在查看的URL在单击星标时不需要更改。

Now, I am wondering what kind of response to return from my star_unstar view - that detects whether the request is being made via AJAX or not.

现在,我想知道从我的star_unstar视图返回什么样的响应 - 检测是否通过AJAX发出请求。

At present, if the request is an AJAX request, it returns just the section of HTML that is needed for the star, so I can replace the HTML in the star's parent DIV, so as the star appears "on" or "off", depending on the user's action.

目前,如果请求是一个AJAX请求,它只返回星号所需的HTML部分,因此我可以替换星号父DIV中的HTML,以便星号显示为“on”或“off”,取决于用户的操作。

However, I would much rather return some kind of JSON object, as it just seems more "proper", I think. The problem with this method is that the javascript would have to modify the star image's src attribute, the href on it, and the link title also, which seems a lot of work for such a simple feature. I am also looking into in-line commenting in the future, but I want to get a feel for how things "should" be done before I start coding lots of JS.

但是,我更愿意返回某种JSON对象,因为它看起来更“合适”,我想。这个方法的问题是javascript必须修改星形图像的src属性,它上面的href和链接标题,这对于这样一个简单的功能似乎很有用。我也在考虑将来的在线评论,但我想了解在开始编写大量JS之前应该如何“完成”。

What is the general consensus when implementing features such as this, not just with Django, but all frameworks that operate in a similar way?

在实现这样的功能时,普遍的共识是什么,不仅仅是Django,还有所有以类似方式运行的框架?

2 个解决方案

#1


7  

When I work with Ajax my main concern is usually to limit the amount of data I have to send. Ajax applications of this type should be very responsive (invisible if possible).

当我使用Ajax时,我主要关心的是限制我必须发送的数据量。这种类型的Ajax应用程序应该非常敏感(如果可能,则不可见)。

In the case of toggling a star, I would create the actual on/off states as CSS classes, StarOn and StarOff. The client will download both the off and on star when they first visit the page, which is acceptable considering that the star is a small image. When you want to change the star appearance in the future, you'll only be editing CSS, and won't have to touch the javascript at all.

在切换星形的情况下,我会将实际的开/关状态创建为CSS类,StarOn和StarOff。客户端在首次访问页面时将同时下载off和on星,考虑到星形是一个小图像,这是可以接受的。如果您想在将来更改星形外观,您将只编辑CSS,而根本不需要触摸javascript。

As for the Ajax, I'd send back and forth one thing -- a JSON variable true/false that says whether or not the request was successful. As soon as the user clicks on the star, I'd change it to the StarOn state and send out the request. 99% of the time Ajax will return true and the user will not even realize that there was some sort of delay in the web request. In the rare case where you get a false back, you'll have to revert the star to StarOff and display an error message to the user.

至于Ajax,我会来回发送一件事 - 一个JSON变量true / false,表示请求是否成功。一旦用户点击星号,我就会将其更改为StarOn状态并发出请求。 99%的时间Ajax将返回true,用户甚至不会意识到Web请求中存在某种延迟。在极少数情况下,您会收到错误的回复,您必须将星形还原为StarOff并向用户显示错误消息。

#2


4  

I don't think your question relates particularly to Django or Python, as you point out at the end.

我不认为你的问题特别与Django或Python有关,正如你在最后指出的那样。

There's a lot of personal preference in whether you return a blob of HTML to write into the DOM or some serialized data as JSON. There are some practical factors you might want to take into account though.

无论是返回一大堆HTML来写入DOM还是一些序列化数据作为JSON,都有很多个人偏好。但是,您可能需要考虑一些实际因素。

Advantages of HTML: - Easy and fast to write straight into the page.

HTML的优点: - 轻松快速地直接写入页面。

Advantages of JSON: - Not coupled to the front-end of your application. If you need that functionality anywhere else in the application, it is there ready to go.

JSON的优点: - 没有耦合到应用程序的前端。如果您在应用程序中的任何其他位置都需要该功能,那么就可以随时使用了。

My call on it. It's only a relatively trivial amount of HTML to update, and I'd probably go for returning JSON in this case and giving myself the extra flexibility that might be useful down the road.

我的呼吁。这只是一个相对微不足道的HTML更新,我可能会在这种情况下返回JSON,并给自己提供额外的灵活性,可能会有所帮助。

#1


7  

When I work with Ajax my main concern is usually to limit the amount of data I have to send. Ajax applications of this type should be very responsive (invisible if possible).

当我使用Ajax时,我主要关心的是限制我必须发送的数据量。这种类型的Ajax应用程序应该非常敏感(如果可能,则不可见)。

In the case of toggling a star, I would create the actual on/off states as CSS classes, StarOn and StarOff. The client will download both the off and on star when they first visit the page, which is acceptable considering that the star is a small image. When you want to change the star appearance in the future, you'll only be editing CSS, and won't have to touch the javascript at all.

在切换星形的情况下,我会将实际的开/关状态创建为CSS类,StarOn和StarOff。客户端在首次访问页面时将同时下载off和on星,考虑到星形是一个小图像,这是可以接受的。如果您想在将来更改星形外观,您将只编辑CSS,而根本不需要触摸javascript。

As for the Ajax, I'd send back and forth one thing -- a JSON variable true/false that says whether or not the request was successful. As soon as the user clicks on the star, I'd change it to the StarOn state and send out the request. 99% of the time Ajax will return true and the user will not even realize that there was some sort of delay in the web request. In the rare case where you get a false back, you'll have to revert the star to StarOff and display an error message to the user.

至于Ajax,我会来回发送一件事 - 一个JSON变量true / false,表示请求是否成功。一旦用户点击星号,我就会将其更改为StarOn状态并发出请求。 99%的时间Ajax将返回true,用户甚至不会意识到Web请求中存在某种延迟。在极少数情况下,您会收到错误的回复,您必须将星形还原为StarOff并向用户显示错误消息。

#2


4  

I don't think your question relates particularly to Django or Python, as you point out at the end.

我不认为你的问题特别与Django或Python有关,正如你在最后指出的那样。

There's a lot of personal preference in whether you return a blob of HTML to write into the DOM or some serialized data as JSON. There are some practical factors you might want to take into account though.

无论是返回一大堆HTML来写入DOM还是一些序列化数据作为JSON,都有很多个人偏好。但是,您可能需要考虑一些实际因素。

Advantages of HTML: - Easy and fast to write straight into the page.

HTML的优点: - 轻松快速地直接写入页面。

Advantages of JSON: - Not coupled to the front-end of your application. If you need that functionality anywhere else in the application, it is there ready to go.

JSON的优点: - 没有耦合到应用程序的前端。如果您在应用程序中的任何其他位置都需要该功能,那么就可以随时使用了。

My call on it. It's only a relatively trivial amount of HTML to update, and I'd probably go for returning JSON in this case and giving myself the extra flexibility that might be useful down the road.

我的呼吁。这只是一个相对微不足道的HTML更新,我可能会在这种情况下返回JSON,并给自己提供额外的灵活性,可能会有所帮助。