django用户配置文件设置与图像上传

时间:2015-01-03 17:07:23

标签: django django-models django-forms django-views

我几天前对django感兴趣,我正在努力建立一个小社交网络来练习。但是我遇到了用户档案的文件上传问题。请在下面找到详细信息:

models.py

from django.db import models
from django.contrib.auth.models import User

def get_image_path(instance, filename):
    return os.path.join('photos', str(instance.id), filename)

class Userprofile(models.Model) :
    user = models.OneToOneField(User, unique=True)
    profile_picture = models.ImageField(upload_to=get_image_path, blank=True)

User.profile = property(lambda x : Userprofile.objects.get_or_create(user=x)[0])

forms.py

from django import forms
from models import Userprofile

class UserProfileForm(forms.ModelForm) :

    class Meta :
        model = Userprofile

views.py

def profilewizard(request) :

    if request.method == 'POST' :    
        print request.POST
        print request.FILES
        form = UserProfileForm(request.POST,request.FILES,instance=request.user.profile)

        if form.is_valid() :
            form.save()

    form = UserProfileForm()
    args = {}
    args.update(csrf(request))
    args['current_user'] = request.user
    args['form'] = form

    return render_to_response('profilewizard.html',args)

我注意到数据库中添加了一个空行,我的控制台显示错误

Exception happened during processing of request from ('127.0.0.1', 46869)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 593, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/local/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 129, in __init__
    super(WSGIRequestHandler, self).__init__(*args, **kwargs)
  File "/usr/lib/python2.7/SocketServer.py", line 651, in __init__
    self.finish()
  File "/usr/lib/python2.7/SocketServer.py", line 710, in finish
    self.wfile.close()
  File "/usr/lib/python2.7/socket.py", line 279, in close
    self.flush()
  File "/usr/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe

我不会理解为什么在数据库中没有存储任何形式有效的

帮助

干杯, Mouha

1 个答案:

答案 0 :(得分:0)

似乎这是一个django知道的bug,无法修复,但上传工作