I am working on a small newsletter-app for a custom-Blog-Django-project (just for me). One main feature of the project is the defined set of Article-Types. All Article-types are children of the abstract base class "Article". Two examples of article-types are "event-article" and "video-article". In the newsletter-app I have a "content"-Field (=email-message). Now I want to choose several articles (of any type) to be included in the newsletter. It may be easier if I just create a function that searches all articles which are not featured in a newsletter yet. Then I would collect all needed information, combine them into a text and set the function as default for the field. But I rather choose the articles by myself. I thought about a m2m-field, but how can I choose some articles (inline in the edit-form of the object) and have the content-field filled with the needed information (like absolute_url or headline) immediately? Thanks for your help in advance.
问问题
365 次
1 回答
0
唯一的选择是 AJAX。设置一个以 JSON 格式返回文章的视图。当用户选择一篇文章时,对该视图进行 AJAX 调用。然后,使用 JavaScript 解析返回的 JSON 并填充文本字段。
视图.py
from django.core import serializers
from django.http import HttpResponse
def json_get_article(request, article_id):
article = get_object_or_404(MyModel, id=article_id)
data = serializers.serialize("json", article)
return HttpResponse(data, mimetype='application/json')
script.js(为简单起见使用 jQuery)
$.getJSON('/my/ajax/url/', function (data, textStatus, jqXHR) {
$('#id_my_text_field').val(data[0]['field_containing_text'])
});
您显然必须对此进行一些调整,但这是基本过程。
于 2011-06-13T18:32:46.790 回答