0

我有一个 Django 应用程序,其中有 Post 和 PostImage 模型。

每个 Post 有多个 PostImage。

在 Post add 页面中,我有 Post 和 PostImages 的表单。我已将表单集用于 PostImages。这是视图的代码:

def post_new(request, template_name="posts/new.html"):


  post_form = Post(request.POST or None, request.FILES or None)
  PostImageFormSet = formset_factory(PostImageForm, extra=1)
  post_image_formset = PostImageFormSet(request.POST or None, request.FILES or None, prefix='post_images')

  if post_form.is_valid() and post_image_formset.is_valid():
    post = post_form.save(commit=False)
    post.save()
    for post_image_form in post_image_formset:
      post_image = post_image_form.save(commit=False)
      post_image.post = post
      post_image.save()

    redirect_url = urlresolvers.reverse('post_show')
    return HttpResponseRedirect(redirect_url)

  form_data = {'post_form': post_form, 'post_image_formset': post_image_formset}
  csrfContext = RequestContext(request, form_data)
  return render_to_response(template_name, csrfContext)

我使用 jQuery 为 PostImage 添加更多表单,它工作得非常好,我可以创建一个包含任意数量图像的帖子。

现在,我希望我的 post_edit 页面以类似的方式工作。这是我的 post_edit 视图方法的代码:

def post_edit(request, post_slug, template_name="developers/posts/edit.html"):
  post = get_object_or_404(post, slug=post_slug)
  post_images = PostImage.objects.filter(post_id=post.id)
  count = len(post_images)
  post_form = postForm(request.POST or None, request.FILES or None, instance=post)
  postImageFormSet = formset_factory(postImageForm, extra=count)
  post_image_formset = postImageFormSet(request.POST or None, request.FILES or None, prefix='post_images')

  for post_image_form, post_image in zip(post_image_formset.forms, post_images):
    post_image_form.initial = post_image
  if post_form.is_valid():
    post_form.save()
    redirect_url = urlresolvers.reverse('developer_post_show', args=[post_slug])
    return redirect(redirect_url)

  form_data = {'post_form': post_form, 'post_image_formset': post_image_formset}
  csrfContext = RequestContext(request, form_data)
  return render_to_response(template_name, csrfContext)

这是我的编辑页面模板:

{% extends "base.html" %}

{% block content %}
{% load thumbnail %}

  {% if error_message %} <p><strong>{{ error_message }}</strong></p> {% endif %}
  {{ post_form.errors }}
  <div style="width: 80%; margin: 0 auto;">
    <form action="" method="post">
      {% csrf_token %}
      <div>
        ... Post related HTML
      </div>

      <div class="post-images" style="clear: both;">
        {% for post_image_form in post_image_formset %}
          <div>
            {{ post_image_form.caption  }}
          </div>
        {% endfor %}
      </div>

      <div style="clear: both; position: relative; left: 320px;">
        <input type="submit" value = "Update post" />
      </div>

    </form>
  </div>
{% endblock %}

我在模板中收到错误消息:

'PostImage' 对象没有属性 'get'

在我的模板中,我迭代了post_image_formset对象,它迭代了 3 次,这是正确的,因为该特定帖子有 3 张图像。但我仍然得到这个错误。任何人都可以帮忙吗?

谢谢。

4

1 回答 1

0

PostImageForm的是一个ModelForm. 在您的编辑视图中,您使用该initial属性来提供现有实例。这不是推荐的方法,您的错误可能是这种情况的症状。

为了提供初始实例QuerySet,使用了 a。

在你的情况下,这看起来像这样:

def post_edit(request, post_slug, template_name="developers/posts/edit.html"):
    post = get_object_or_404(post, slug=post_slug)
    post_images = PostImage.objects.filter(post_id=post.id)

    PostImageFormSet = formset_factory(postImageForm, extra=2)
    formset = PostImageFormSet(queryset=post_images)

您可能还想使用推荐的方式来处理表单视图中的 POST 与 GET 请求

于 2012-06-20T11:32:51.227 回答