Django:图片上传 "This field is required"

Django : Image upload "This field is required"

我尝试使用 django 上传图片。但我得到了错误。

models.py

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    phone = models.CharField(max_length=10)
    college = models.CharField(max_length=40)
    pic = models.ImageField(upload_to='images/pics')

views.py

def register(request):
    registered = False
    context = RequestContext(request)
    if request.method == 'POST':
        user_form = UserForm(request.POST)
        profile_form = ProfileForm(request.POST,request.FILES)

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()

            up = UserProfile(user=user,pic=request.FILES['pic'],phone=request.POST['phone'],college=request.POST['college'])

            up.save()

            registered =True
        else:
            print(user_form.errors, profile_form.errors)
            print("error")
    else:
        user_form = UserForm()
        profile_form=ProfileForm()

    return render_to_response('join/register.html',
        {'user_form':user_form,'profile_form':profile_form,'registered':registered},
        context)

urls.py

  urlpatterns = [
        url(r'^join/$',join_views.register, name='register'),
        url(r'^$',join_views.jlogin, name='login'),

        ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

html

<form id="user_reg" method="post" action="." enctype="multipart/form-data">
<fieldset>
    <legend>Join with US</legend>
    {% csrf_token %}
    <div>
        <table>
           <tr> <th><label for="user">User Name</label></th> <td>{{ user_form.username }}</td></tr>
            <tr> <th><label for="email">Email</label></th> <td>{{ user_form.email }}</td></tr>
            <tr> <th><label for="password">Password</label></th> <td>{{ user_form.password }}</td></tr>
            <tr> <th><label for="phone">Phone</label></th> <td>{{ profile_form.phone }}</td></tr>
            <tr> <th><label for="college">College</label></th> <td>{{ profile_form.college }}</td></tr>
            <tr> <th><label for="pic">Picture</label></th> <td>{{ profile_form.pic }}</td></tr>

        </table>

    </div>
    <br>

 <input type="submit" value="Register">
 </fieldset>
</form>
{%  endif %}

form.py

class UserForm(forms.ModelForm):
    username = forms.CharField(widget=forms.TextInput(attrs={'class':'txtbox'}))
    password = forms.PasswordInput()
    pic = forms.ImageField(help_text=False);

    class Meta:
        model = User
        fields = ('username','email','password')

class ProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields= ('phone','college','pic')

这是我上传图片的例子。 我的设置 MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/'

正在提交。我收到类似“{'pic': [u'This field is required.']}, {})”的错误 谁能解释我如何上传图片?? 预先感谢您的帮助!

两种方式:

  1. 为什么要在 UserForm 中定义 pic?直接删除

  2. 或者只在 user_form 中包含 request.FILES(错误的方式)