无法在management_form中发送少量图像,图像丢失

时间:2017-06-28 10:30:55

标签: jquery python django forms formset

我有django应用程序(django 1.6),我有问题发送少量图像,上传少量图像。 在前端这是可以的,我可以拖放图像并在那里看到它们。

按下保存按钮时问题开始了。从前端上传的少量图片,现在我只能看到一个。因此,例如,从前端的4个上传图像,现在我只能看到一个。

我不知道哪里可能有问题。 我会感谢任何帮助。

我的网址:

url('^post/create/$', PostAdd.as_view(), name="event_create"),

我的观点:

class PostAdd(BasePostEdit):
    permission_required = 'calendars.can_create_posts'

Paretnt观点:

class BasePostEdit(PermissionRequiredMixin, FormView):
    form_class = PostForm
    template_name = "calendars/post_form.html"
    success_url = reverse_lazy('home')

    def get_permission_object(self):
        return self.request.session["current_calendar"]

    def get_context_data(self, form):
        context = super(BasePostEdit, self).get_context_data()

        if form.instance.pk:
            # if we get an url to edit a post set the current calendar to posts
            # calendar
            self.request.session["current_calendar"] = form.instance.calendar
            # update seen by
            PostSeenBy.update_seen_by([form.instance, ], self.request.user)

        cur_calendar = self.request.session["current_calendar"]
        locales = []
        initial = {'calendar': cur_calendar}

        for loc in cur_calendar.locales.all():
            if not loc.pk in form.instance.locales.values_list("locale", flat=True):
                locales.append({'locale':loc})

        if not form.instance.pk:
            AllLocaleFormset = inlineformset_factory(Post, PostLocale, formset=BasePostLocaleFormset, extra=cur_calendar.locales.count())
            formset = AllLocaleFormset(initial=locales, instance=form.instance)
        else:
            PostLocaleFormset = inlineformset_factory(Post, PostLocale, formset=BasePostLocaleFormset, extra=len(locales))
            formset = PostLocaleFormset(queryset=form.instance.locales.all(), initial=locales, instance=form.instance)

        if self.request.GET.get('pub_date', None):
            date = datetime.strptime(self.request.GET.get('pub_date'), "%d/%m/%Y")
        elif form.instance.pub_date:
            import pytz
            date = form.instance.pub_date.astimezone(pytz.timezone(settings.TIME_ZONE))
        else:
            # date = date.today()
            date = now().date()

        context['back_url'] = reverse_lazy('home_month', kwargs={
            'calendar': cur_calendar.slug,
            'year': date.year,
            'month': date.month})

        context['form'] = form
        context['post'] = form.instance
        context['current_calendar'] = self.request.session["current_calendar"]
        context['locales'] = formset
        return context

以下是我的HTML的一部分,我认为下面的问题部分是{% get_upload_forms post locale.nested %},是来自get_upload_forms的代码:

{% extends "base.html" %}
{% load static calendars_tags guardian_tags %}

{% block body-tag-classes %}skinny-header{% endblock body-tag-classes %}

{% block content %}
{% get_obj_perms user for current_calendar as "perm_list" %}
<form action="{% if post.pk %}{% url 'event_edit' post.pk %}{% else %}{% url 'event_create' %}{% endif %}" method="POST" enctype="multipart/form-data">
  {% csrf_token %}
  <fieldset {% if 'can_edit_posts' not in perm_list %}disabled="disabled"{% endif %}>
      <input type="hidden" name="delete" value="" />

        {{ locales.management_form }}
        {% for locale in locales.forms %}
          <p>Locale {{ locale.locale.value|locale_code }}</p>
          {{ locale.description }}
          {{ locale.calendar }}
          {{ locale.id }}
          {{ locale.locale }}
          {% get_upload_forms post locale.nested %}
        {% endfor %}

      {{ form.version }}
  </fieldset>

get_upload_forms代码

@register.inclusion_tag('calendars/includes/upload_forms.html', takes_context=True)
def get_upload_forms(context, post, forms):
    perm_list = context.get('perm_list', None)
    return locals()

这是upload_forms.html的html:

{% load thumbnail %}
<ul class="uploads">
{% for formset in forms%}
    {{ formset.management_form }}
    {% for a in formset %}
        {% if 'can_edit_posts' in perm_list or a.attachment.value %}
        <li class="{% if not a.attachment.value %}empty{% endif %}" data-form-name="{{ a.attachment.html_name }}">
            {{ a.id }}{{ a.post_locale }}
            <div class="preview">
              <div class="action">
                <span class="icon-hospital">Add Image</span>
              </div>
                <img src="{% thumbnail a.attachment.value 400x400 autocrop=False %}" alt="{{ a.attachment.value.name }}">
              <input class="upload-media" name="{{ a.attachment.html_name }}" type="file" value="{{ a.attachment.name }}" multiple>
              <input type="hidden" class="files-to-exclude">
            </div>

            <div class="description">
              <div class="filename">{{ a.attachment.value.name }}</div>
              <div class="filesize">{{ a.attachment.value.size }}</div>
              <ul class="btn-group">
                <li class="download">
                  <a class="button icon-download-alt" href="{{ a.attachment.value.url }}" target="_blank"></a>
                </li>
              {% if formset.can_delete and 'can_edit_posts' in perm_list %}
                <li class="delete">
                  {{ a.DELETE }}
                  <label for="{{a.DELETE.auto_id}}" class="button icon-trash"></label>
                </li>
              {% endif %}
              </ul>
            </div>
            <div class="previews"></div>
        </li>
      {% endif %}
    {% endfor %}
{% endfor %}
</ul>

编辑:这是来自firebug的代码

<ul class="uploads">

    <input id="id_FILES_4950-TOTAL_FORMS" name="FILES_4950-TOTAL_FORMS" type="hidden" value="3"><input id="id_FILES_4950-INITIAL_FORMS" name="FILES_4950-INITIAL_FORMS" type="hidden" value="2"><input id="id_FILES_4950-MAX_NUM_FORMS" name="FILES_4950-MAX_NUM_FORMS" type="hidden" value="1000">


        <li class="" data-form-name="FILES_4950-0-attachment">
            <input id="id_FILES_4950-0-id" name="FILES_4950-0-id" type="hidden" value="4485"><input id="id_FILES_4950-0-post_locale" name="FILES_4950-0-post_locale" type="hidden" value="4950">
            <div class="preview">
              <div class="action">
                <span class="icon-hospital">Add Image</span>
              </div>
                <img src="/media/localefile/16010645_1347977721942284_325286987_o_155.jpg.400x400_q85.jpg" alt="localefile/16010645_1347977721942284_325286987_o_155.jpg">
              <input class="upload-media" name="FILES_4950-0-attachment" type="file" value="attachment" multiple="">
              <input type="hidden" class="files-to-exclude">
            </div>

            <div class="description">
              <div class="filename">localefile/16010645_1347977721942284_325286987_o_155.jpg</div>
              <div class="filesize">60651</div>
              <ul class="btn-group">
                <li class="download">
                  <a class="button icon-download-alt" href="/media/localefile/16010645_1347977721942284_325286987_o_155.jpg" target="_blank"></a>
                </li>

                <li class="delete">
                  <input id="id_FILES_4950-0-DELETE" name="FILES_4950-0-DELETE" type="checkbox">
                  <label for="id_FILES_4950-0-DELETE" class="button icon-trash"></label>
                </li>

              </ul>
            </div>
            <div class="previews"></div>
        </li>



        <li class="" data-form-name="FILES_4950-1-attachment">
            <input id="id_FILES_4950-1-id" name="FILES_4950-1-id" type="hidden" value="4486"><input id="id_FILES_4950-1-post_locale" name="FILES_4950-1-post_locale" type="hidden" value="4950">
            <div class="preview">
              <div class="action">
                <span class="icon-hospital">Add Image</span>
              </div>
                <img src="/media/localefile/16010645_1347977721942284_325286987_o_156.jpg.400x400_q85.jpg" alt="localefile/16010645_1347977721942284_325286987_o_156.jpg">
              <input class="upload-media" name="FILES_4950-1-attachment" type="file" value="attachment" multiple="">
              <input type="hidden" class="files-to-exclude">
            </div>

            <div class="description">
              <div class="filename">localefile/16010645_1347977721942284_325286987_o_156.jpg</div>
              <div class="filesize">60651</div>
              <ul class="btn-group">
                <li class="download">
                  <a class="button icon-download-alt" href="/media/localefile/16010645_1347977721942284_325286987_o_156.jpg" target="_blank"></a>
                </li>

                <li class="delete">
                  <input id="id_FILES_4950-1-DELETE" name="FILES_4950-1-DELETE" type="checkbox">
                  <label for="id_FILES_4950-1-DELETE" class="button icon-trash"></label>
                </li>

              </ul>
            </div>
            <div class="previews"></div>
        </li>



        <li class="empty" data-form-name="FILES_4950-2-attachment">
            <input id="id_FILES_4950-2-id" name="FILES_4950-2-id" type="hidden"><input id="id_FILES_4950-2-post_locale" name="FILES_4950-2-post_locale" type="hidden" value="4950">
            <div class="preview">
              <div class="action">
                <span class="icon-hospital">Add Image</span>
              </div>
                <img src="" alt="">
              <input class="upload-media" name="FILES_4950-2-attachment" type="file" value="attachment" multiple="">
              <input type="hidden" class="files-to-exclude">
            </div>

            <div class="description">
              <div class="filename"></div>
              <div class="filesize"></div>
              <ul class="btn-group">
                <li class="download">
                  <a class="button icon-download-alt" href="" target="_blank"></a>
                </li>

                <li class="delete">
                  <input id="id_FILES_4950-2-DELETE" name="FILES_4950-2-DELETE" type="checkbox">
                  <label for="id_FILES_4950-2-DELETE" class="button icon-trash"></label>
                </li>

              </ul>
            </div>
            <div class="previews"></div>
        </li>



</ul>

0 个答案:

没有答案