Django:图片上传“此字段是必填项”

时间:2015-01-22 14:04:09

标签: django django-models django-forms

我尝试使用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,&#39; media&#39;) MEDIA_URL =&#39; / media /&#39;

提交时。我得到的错误就像&#34; {&#39; pic&#39;:[u&#39;此字段是必填字段。&#39;]},{})&#34; 任何人都可以解释我如何上传图像? 提前感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

两种方式:

  1. 为什么要在pic中定义UserForm?只需删除它

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