我有一个我在本地计算机上构建和测试的Django站点,它运行正常(runserver命令)。当我将它上传到我的主机时,一切都加载得很好但我有一个问题,表单不会更新并重定向到索引页面。奇怪的是,当它执行此操作时,它会保留我在表单上的上一页的URL,然后表单不会更新。
此问题影响了两种形式。我发现的是,在我的托管网站上,任何带有图像字段的表单都不会更新并转到索引页面。
他们被设置为ImageFields,我确实发现Pillow没有安装,我无法正确安装它。所以我将字段更改回FileFields并仍然存在相同的问题。
如果我从forms.py表单中删除FileFields,表单会正确更新。
所以我肯定知道它是FileField。
我所使用的主机具有可怕的日志记录,并且没有通过命令行进行测试的ssh访问。
来自settings.py的媒体/静态设置(图片在静态和媒体的网站上加载得很好,所以我相信这些都是正确的):
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '..','autox','static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, '..','autox','media')
Forms.py
#user profile form
class ProfileForm(forms.ModelForm):
team_manager = forms.BooleanField(disabled=True, required=False)
photo = forms.FileField(required=False)
class Meta:
model = Member
fields = ['team','team_manager','bio','position','photo','phone','twitter','facebook','instagram']
help_texts = {
'team': 'If your team is not listed here, it can be created later under the user menu',
'team_manager': 'You cannot modify this value but if it is checked, you can update the team profile',
}
#Team form
class TeamForm(forms.ModelForm):
class Meta:
model = Team
fields = ['name','department','location','team_contact_email','team_contact_number','theme_song','skill_level','team_photo','team_bio','website']
urls.py
...
url(r'^updateteam/$', views.updateteam, name='updateteam'),
url(r'^accounts/profile/$', views.profile, name='profile'),
...
views.py
#profile update (issue 1)
@login_required
def profile(request):
member = Member.objects.get(name=request.user)
user = User.objects.get(username=request.user.username)
if request.method == 'POST':
uf = UserEditForm(request.POST, instance=user)
pf = ProfileForm(request.POST,request.FILES, instance=member)
if uf.is_valid() * pf.is_valid():
user = uf.save()
memberprofile = pf.save(commit=False)
photo = request.FILES.get('photo', False)
if photo:
memberprofile.photo = photo
memberprofile.name = user
memberprofile.email = user.email
if 'team' in pf.changed_data:
memberprofile.team_manager = False
memberprofile.save()
return render(request,'registration/profile_updated.html')
else:
uf = UserEditForm(instance=user)
pf = ProfileForm(instance=member)
return render(request,'registration/profile.html', {'userform':uf,'profileform':pf})
#team update (issue 2)
@login_required
def updateteam(request):
member = Member.objects.get(name=request.user)
if request.method == 'POST':
form = TeamForm(request.POST, request.FILES, instance=member.team)
if form.is_valid():
team = form.save(commit=False)
photo = request.FILES.get('team_photo',False)
if photo:
team.team_photo = photo
team.save()
return redirect('packages')
else:
return redirect('teams')
else:
form = TeamForm(instance=member.team)
return render(request, 'autox/teammanagement.html',{'form':form})
最后是Models.py
#Member (profile issue 1)
class Member(models.Model):
name = models.ForeignKey(User)
team = models.ForeignKey(Team,models.SET_NULL,blank=True,null=True) #set_null does not delete the user when deleting the team
team_manager = models.BooleanField(default=False)
bio = models.TextField(blank=True)
photo = models.FileField(upload_to='members',blank=True, default='members/no-photo.png')
email = models.CharField(max_length=255,blank=True)
phone = models.CharField(max_length=255,blank=True)
twitter = models.CharField(max_length=255, blank=True)
facebook = models.URLField(blank=True)
instagram = models.URLField(blank=True)
#Team (issue 2)
class Team(models.Model):
name = models.CharField(max_length=255)
department = models.CharField(max_length=255,blank=True)
location = models.CharField(max_length=255,blank=True)
team_contact_email = models.CharField(max_length=255,blank=True)
team_contact_number = models.CharField(max_length=255,blank=True)
theme_song = models.CharField(max_length=255,blank=True)
team_photo = models.FileField(blank=True, upload_to='team', default='team/no-photo.png')
team_bio = models.TextField()
website = models.URLField(blank=True)
所以,我对我的FileField问题非常有信心,我只是看不出有什么问题,因为它在我的本地机器上工作,而不是在服务器上。
感谢帮助!