Python 如何更新用户';Django中的s模型字段?
我目前正在用Django创建一个社交平台。现在,我正在开发配置文件设置页面,希望最终用户能够更改标题和显示图像 但是,我无法使表单正常工作。标题和显示图像都正确地通过POST表单,并最终存储在媒体根目录中,从那里我调用模板中的所有标题和显示图像 因此,现在的问题是将提交的图像引用为当前用户的标题或显示图像。这是我的Profiel(荷兰语表示Profile)Models.py:Python 如何更新用户';Django中的s模型字段?,python,django,forms,models,Python,Django,Forms,Models,我目前正在用Django创建一个社交平台。现在,我正在开发配置文件设置页面,希望最终用户能够更改标题和显示图像 但是,我无法使表单正常工作。标题和显示图像都正确地通过POST表单,并最终存储在媒体根目录中,从那里我调用模板中的所有标题和显示图像 因此,现在的问题是将提交的图像引用为当前用户的标题或显示图像。这是我的Profiel(荷兰语表示Profile)Models.py: class Profiel(models.Model): user = models.OneToOneField(Use
class Profiel(models.Model):
user = models.OneToOneField(User, primary_key=True)
Profielfoto = models.ImageField(blank=True, upload_to="profile_image", default="profile_image/no_profile_pic.jpg")
Profielheader = models.ImageField(blank=True, upload_to="profile_header")
Biografie = models.CharField(blank=True, max_length=150, default="Biografie nog niet ingevuld.")
def __str__(self):
return self.user.username
def create_profile(sender, **kwargs):
if kwargs['created']:
user_profile = Profiel.objects.create(user=kwargs['instance'])
post_save.connect(create_profile, sender=User)
我的观点
@login_required
def profile_edit_view(request):
if request.method == 'POST':
form = Edit_profile_forms(request.POST, request.FILES)
if form.is_valid():
form.save()
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('profiel_edit'))
else:
form = Edit_profile_forms(request.POST, request.FILES)
args = {'form':form}
return render(request, 'Dashboard/profiel_edit.html', args)
和我的forms.py:
class Edit_profile_forms(forms.ModelForm):
class Meta:
model = Profiel
fields = (
'Profielfoto',
'Profielheader',
'Biografie',
)
我认为这些是唯一需要向你们展示解决这个问题的方法,因为表单正在正确地处理其他所有事情。所以我的最后一个问题是:
如何将当前用户的标题/图像文件更新为表单中提交的文件?Iv编辑了我的答案,以便您可以复制并粘贴到视图中
@login_required
@csrf_protect
def profile_edit_view(request):
template = 'Dashboard/profiel_edit.html'
qv = Profiel.objects.get(user=request.user.id)
if request.method == 'POST':
form = Edit_profile_forms(request.POST, request.FILES, instance=qv)
if form.is_valid():
form.save()
return HttpResponseRedirect(some_view)
else:
print form.errors
else:
form = Edit_profile_forms(instance=qv)
return render(request, template, {'form':form})
更新
我知道你想让用户通过表单上传新图片来编辑他们的个人资料/标题图片。您的代码只是通过表单从数据中保存新数据,而不是更新现有数据
Django具有基于类的视图。
从django.views.generic.edit导入更新视图
class UpdateExampleView(UpdateView):
model = your model
form_class = your Form
template_name = 'your template'
success_url = path to go when form successfully passed
# additionally you can handle your data with these methods
def get_form_kwargs(self):
def get_context_data(self, **kwargs):
def form_valid(self, form):
别忘了,你们需要像这样通过url给出pk号
url(r'^example/path/(?P<pk>[0-9]+)/$', your class view or function view, name='your name'),
你说你的图像保存在media root中是正确的
所以,如果您在数据库中检查Profiel表,
在Profielfoto、ProfielHeader字段中,将有路径
例如,如果您的profilephoto的名称为“profiletest.jpg”,
Profielfoto字段中的数据将是“profile\u image/profiletest.jpg
”。
因为您在模型类中设置了upload\u to=“profile\u image”
如果要从视图(模板)中的媒体文件夹加载图像,
你这样写
<img src="{{MEDIA_URL}}{{yourimagepath}}"/>
我给了它一个和你一样的例子,但仍然是:它只是上传到媒体根目录,没有更新当前的配置文件/标题图像。很抱歉,你误解了我的问题。我在将图片加载到前端用户时没有问题。因为他们当前的照片显示正确。问题是,我的表单没有将其当前配置文件/标题图像更新为表单中提交的新图像。明白了。你检查过你的数据库了吗?当用户在表单上上载新图片时,您想更改配置文件/标题图像,对吗?当我看到您的代码时,它将在数据库中生成新数据,而不是更新现有数据。我会更新我的答案,请检查我是否正确解决了您的问题。
<img src="{{MEDIA_URL}}{{yourimagepath}}"/>
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.template.context_processors.media',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
...