2015-01-03 96 views
0

我前幾天對django感興趣,我正在嘗試爲練習建立一個小社交網絡。但是我遇到了用戶配置文件上傳的問題。請看以下細節: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 

我不understatnd爲什麼沒有被存儲在數據庫中althought形式是有效的

THX的幫助

乾杯, Mouha

+0

以下是一個很好的資源,可以查看如何使用文件圖像上傳創建表單 - > http://www.tangowithdjango.com/book/chapters/的login.html#的用戶模型 –

+0

thx它幫助了很多我的pb是空白=真我刪除它它的工作就像一個魅力 – user3142208

回答

0

現在看來,這是一個Django知道錯誤,將不會被修復,但上傳作品