5

当我尝试提交表单时,我一直试图弄清楚出了什么问题。当我在表单上点击提交时,我在网页上收到此响应:

Image:
    This field is required.

似乎认为我没有在表格中包含图像!

设置.py

...
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': '/path/to/myproject/database/sqlite.db'),
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}
...
MEDIA_ROOT = '/path/to/myproject/media/'
MEDIA_URL = '/media/'
...
INSTALLED_APPS = (
    ...
    'myapp',
)

模型.py

from PIL import Image
from django import forms
from django.db import models
from django.forms import ModelForm

UPLOADER_CHOICES = (
    ('C', 'Common User'),
    ('N', 'Nutrition Kitchen'),
)

class Meal(models.Model):

    title       = models.CharField(max_length=100)
    slug        = models.SlugField(unique=True)
    image       = models.ImageField(upload_to='images/')
    serves      = models.IntegerField()
    ingredients = models.CharField(max_length=1000)
    instructions= models.CharField(max_length=1000)
    time_period = models.IntegerField()# in minutes
    uploader    = models.CharField(max_length=1, choices=UPLOADER_CHOICES)

def __unicode__(self):
    return self.title

表格.py

from django import forms
from meal.models import Meal

class MealForm(forms.Form):

    title       = forms.CharField(max_length=100)
    image       = forms.ImageField()
    ingredients = forms.CharField(max_length=1000)
    instructions= forms.CharField(max_length=1000)
    time_period = forms.IntegerField(required=False)
    serves      = forms.IntegerField(required=False)

视图.py

from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from meal.models import Meal
from meal.forms import MealForm

def upload(request):
    if request.method == 'POST':
        form = MealForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return render_to_response('upload.html', {'form': form})
    else:
        form = MealForm()
    return render_to_response('upload.html', {'form': form}, context_instance=RequestContext(request)
    )

上传.html

{% extends "base.html" %}
{% block content %}
{% if form.errors %}
        <p style="color: red;">
            Please correct the error{{ form.errors|pluralize }} below.
        </p>
{% endif %}
<div data-role="content" style="padding: 15px">
    <form action="" method="POST" enctype="multipart/form-data">
            {% csrf_token %}
            {{ form.errors }}
            {{ form.non_field_errors }}
            {{ form.as_table }}
        <input type="submit" value="Submit" id="Save">
    </form>
</div>
{% endblock %}

有没有人知道我可能做错了什么?我觉得我什么都试过了。

4

6 回答 6

7

好的,事实证明这是 jQuery Mobile 的问题,我之前没有提到。我需要做的就是改变

<form action="" method="post" enctype="multipart/form-data">

<form action="" method="post" enctype="multipart/form-data" data-ajax="false">

因为 jQuery 试图为我处理 POST。显然,当您在 Django 中发布到相同的 URL 并想要手动处理它时,您必须包含

data-ajax="false"
于 2012-10-14T19:35:03.903 回答
1

在你的 forms.py 中试试这个

image = forms.ImageField(widget=forms.FileInput())

我希望它有帮助

于 2012-10-10T07:20:53.283 回答
1

在您的模型中,您有

models.ImageField(upload_to='images/')

此字段是必需的。将其更改为

models.ImageField(upload_to='images/', blank=True, null=True)

将使其不需要

于 2012-10-11T00:53:48.573 回答
1

这一行:

form.save()

...什么也没做。您应该要么处理来自表单的cleaned_data,要么使用具有save() 方法的ModelForm。

Django 表单| Django 模型表单

于 2012-10-11T02:37:50.673 回答
0

看来你错过了这个,

class MealForm(forms.Form):
    class Meta:
        model = Meal 
    title = forms.CharField(max_length=100)
于 2012-10-10T06:00:08.313 回答
0

尝试使用模型表单而不是常规表单

class MealForm(forms.ModelForm): #Note, this is a Modelform, not a regular old form.
    class Meta: 
        model = Meal
于 2012-10-10T20:45:32.720 回答