带有图像上传的django用户配置文件设置
django user profile setup with image upload
几天前我对 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
我不明白为什么虽然表格有效但数据库中没有存储任何内容
感谢帮助
干杯,
牟哈
这似乎是一个 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
我不明白为什么虽然表格有效但数据库中没有存储任何内容
感谢帮助
干杯, 牟哈
这似乎是一个 django 知道的错误,不会被修复,但上传有效