在Django中提交表单时无法正确过帐
我正在尝试将文件(特别是图像)上载到数据库。当我试图在在Django中提交表单时无法正确过帐,django,django-forms,django-views,Django,Django Forms,Django Views,我正在尝试将文件(特别是图像)上载到数据库。当我试图在profile\u photo.html中发布我的表单时(附在下面),它会给我一个错误提示: 在/user\u profile/login/upload\u profile\u photo/ profile_photo.html: <body> <div id="blue_background_with_fibers"> <form class="pull-center white form-signin
profile\u photo.html
中发布我的表单时(附在下面),它会给我一个错误提示:
在/user\u profile/login/upload\u profile\u photo/
profile_photo.html:
<body>
<div id="blue_background_with_fibers">
<form class="pull-center white form-signin" role="form" action="" method="POST" enctype="multipart/form-data">
{% csrf_token %} {{ form.as_p }}
<button class="aqua button2" type="submit" value="OK">Upload</button>
</form>
</div>
<div class="modal fade" id="profile" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Upload Profile Picture</h4>
</div>
<div class="modal-body">
<form>
<!--Insert form here-->
<iframe src="upload_profile_photo/" allowTransparency="true" scrolling="yes" frameborder="0" width="560px" height="175px"></iframe>
</form>
</div>
<div class="modal-footer">
<span>Ekho © 2016</span>
</div>
</div>
</div>
</div>
Models.py:
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile');
background = models.FileField(upload_to = 'user_profile/media/', blank=True, null=True);
profile = models.FileField(upload_to = 'user_profile/media/', blank=True, null=True);
about = models.TextField(default='', blank=True);
reviews = models.TextField(default='', blank=True);
def __str__(self):
return self.user.username
最后是url.py:
from django.conf.urls import url
from . import views
app_name = 'user_profile'
urlpatterns = [
# /user_profile/
url(r'^$', views.index, name='index'),
# /user_profile/username
url(r'^user_profile/detail/$', views.detail, name='detail'),
# user_profile/register/
url(r'^register/$', views.RegisterFormView.as_view(), name='register'),
# user_profile/login/
url(r'^login/$', views.LoginFormView.as_view(), name='login'),
url(r'^login/upload_profile_photo/$', views.EditProfileView.as_view(), name='edit_profile')
]这可能是问题所在
在views.py文件中,您正试图访问“profile_photo”,根据models.py,该文件应位于“profile”中
views.py
user.profile_photo = request.FILES['profile_photo']
user.profile_photo = request.FILES['profile']
当您试图访问Dict中不可用的密钥时,会发生此错误多值DICTKEYERROR
我想这应该行得通
views.py
user.profile_photo = request.FILES['profile_photo']
user.profile_photo = request.FILES['profile']
但这可能是问题所在
在views.py文件中,您正试图访问“profile_photo”,根据models.py,该文件应位于“profile”中
views.py
user.profile_photo = request.FILES['profile_photo']
user.profile_photo = request.FILES['profile']
当您试图访问Dict中不可用的密钥时,会发生此错误多值DICTKEYERROR
我想这应该行得通
views.py
user.profile_photo = request.FILES['profile_photo']
user.profile_photo = request.FILES['profile']
PyMentor有一个在django中发布带有图像的表单的教程。
PyMentor有一个教程,可以在django中发布带有图像的表单。
对于第二个问题:
唯一约束失败:用户\u配置文件\u用户配置文件。用户\u id
您不能使用form.save
检索UserProfile
实例。在调用表单后,它将创建另一个用户配置文件对象。保存,因此您将得到唯一约束错误
尝试用以下内容替换表单。保存:
user = User.objects.get(pk=request.user.pk).profile
对于第二个问题:唯一约束失败:user\u profile\u userprofile.user\u id
您不能使用form.save
检索UserProfile
实例。在调用表单后,它将创建另一个用户配置文件对象。保存,因此您将得到唯一约束错误
尝试用以下内容替换表单。保存:
user = User.objects.get(pk=request.user.pk).profile
请你也附上你的堆叠轨迹好吗?这将有助于调查哪一行提示您错误检查文件类型的代码实际上应该是表单本身,使用clean_fieldname方法。@Enix您可以在此处查看堆栈状态:您有两个文件要上载,对吗?你是同时上传的吗?错误似乎来自语句request.FILES['profile']
,在request.FILES
dict.@Enix中找不到密钥。事实上,我忘记了同时上载这两个文件。但是现在我遇到了另一个错误,它无法在此处保存与帐户关联的用户:这是它返回的错误:UNIQUE constraint failed:user\u profile\u userprofile.user\u id
请也附加堆栈跟踪?这将有助于调查哪一行提示您错误检查文件类型的代码实际上应该是表单本身,使用clean_fieldname方法。@Enix您可以在此处查看堆栈状态:您有两个文件要上载,对吗?你是同时上传的吗?错误似乎来自语句request.FILES['profile']
,在request.FILES
dict.@Enix中找不到密钥。事实上,我忘记了同时上载这两个文件。但是现在我遇到了另一个错误,它无法在此处保存与帐户关联的用户:这是它返回的错误:UNIQUE constraint failed:user\u profile\u userprofile.user\u id
感谢您指出这个错误,解决了这个多关键字错误,但是,我现在遇到了一个新问题,它无法保存所做的更改。当我在views.py
中写入user.save()
时会发生这种情况。它返回的具体错误是UNIQUE constraint failed:user\u profile\u userprofile.user\u id
感谢您指出这个错误,解决了MultiKeyDictError,但是我现在遇到了一个新问题,它无法保存所做的更改。当我在views.py
中写入user.save()
时会发生这种情况。它返回的特定错误是唯一约束失败:user\u profile\u userprofile.user\u id