0

我想在我的 django 应用程序中保存和过滤用户对象。输入以下代码后,图像字段不断给我一个验证错误,说:

                         This field is required.

它指向我应该填充它的图像域。如何摆脱该错误并使其过滤?

楷模

      class Fin(models.Model):
         user=models.ForeignKey(User)
         title=models.CharField(max_length=250)
         main_view=models.ImageField(upload_to="photos")
         side_view=models.ImageField(upload_to="photos")
         address=models.CharField(max_length=200)
         city=models.CharField(max_length=200)
         state=models.CharField(max_length=200)
         guideline=models.TextField(max_length=1000)

         def __unicode__(self):
              return self.title

        def get_absolute_url(self):
            return self.title

    class FinForm(ModelForm):
        class Meta:
           model=Fin
           fields=('title','main_view','side_view', 'address','city','state','guideline')
           exclude=('user')

意见

        def fincrib(request):
            extra_data_context={}
             #if there's nothing in the field do nothing.
            if request. method=="POST":
               form =FinForm(request.POST)
               if form.is_valid():
                  data=form.cleaned_data
                  newfincribs=Fin(
                     user= request.user,
                     title=data['title'],
                     main_view=Fin.objects.latest['main_view'],
                     side_view=Fin.objects.latest['side_view'],
                     address=data['address'],
                     city=data['city'],
                     state=data['state'],
                     guideline=data['guideline'])
                 newfincribs.save()
              extra_data_context.update({'FinForm':form})
        else:
            form = FinForm()
            extra_data_context.update({'FinForm':form})
        extra_data_context.update({'Fins':Fin.objects.filter(user=request.user)})

        plan=Fin.objects.filter(user=request.user)
        paginator=Paginator(plan, 5)

        try:
          page=request.GET.get('page', '1')
        except ValueError:
          page=1

        try:
          Fins=paginator.page(page)
        except (EmptyPage, InvalidPage):
          Fins=paginator.page(paginator.num_pages)
       extra_data_context.update({'Fins': Fins})
       return render_to_response('post.html',extra_data_context,context_instance=RequestContext(request))

模板

        {% block content %}
          <form action="." method="POST">
               {% csrf_token %}
       <center> {{FinForm.as_p}}  </center>
           <input type="submit" value="Submit"/>
           </form>


          {% for Fin in Fins.object_list %}
           <tr>
            <a href="{% url profiles_edit_profile %}"> {{Fin.user}} </a> </p> </strong>
             <p>{{Fin.title}}</p>
             <p><img src="{{MEDIA_URL}}/{{Fin.main_view}}"/></p>
             <p> <img src="{{MEDIA_URL}}/{{Fin.side_view}}"/></p>
             <p> {{Fin.address}} </p>
             <p> {{Fin.city}}</p>
              <p> {{Fin.state}}</p>
              <p> {{Fin.guideline}}</p>
            {% endfor %}

     <div class="pagination">
       <span class="step-links">
           {% if Fins.has_previous %}
              <a href="?page={{ Fins.previous_page_number }}">previous</a>
           {% endif %}

    <span class="current">
      Page {{ Fins.number }} of {{ Fins.paginator.num_pages }}
    </span>

         {% if Fins.has_next %}
             <a href="?page={{ Fins.next_page_number }}">next</a>
         {% endif %}
       </span>
      </div>
     {% endblock %}
4

2 回答 2

0

这是因为默认情况下所有模型字段都是必需的,这意味着如果要在数据库中创建和保存新模型实例,则应填写所有必填字段。也许

main_view=Fin.objects.latest['main_view'],
side_view=Fin.objects.latest['side_view'],

给你错误,因为没有数据。

于 2012-06-05T16:47:12.967 回答
-1

改变

main_view=models.ImageField(upload_to="photos")
side_view=models.ImageField(upload_to="photos")

main_view=models.ImageField(upload_to="photos", blank=True, null=True)
side_view=models.ImageField(upload_to="photos", blank=True, null=True)

顺便说一句,我不确定您使用的是哪个 django 版本,但您的代码相当混乱,您有什么理由不使用 CBV 和静态标签?

于 2012-06-05T16:42:31.530 回答