使用情況是一個登錄使用,可以調出他們的個人資料到表單中,編輯它使它在數據庫中更新。Django的不提交表單數據庫
與下面的正確資料數據的代碼能夠被觀看。它也可以被調用成可編輯的形式。表單允許編輯數據,但是在提交數據時,數據庫不會更新並顯示原始配置文件。
我用盡了一切辦法,包括控制檯打印報表,所以我知道它是如何到達代碼的POST部分,是一個有效的形式,做最初的保存()。
任何見解將不勝感激(我使用Django 1.11)。我不想開始運行一隻兔子,但我唯一沒有嘗試過的方法是斷開與用戶的OneToOne關係並使用foriegnkey,因爲我想在用戶註冊時創建配置文件條目。
注: 當我突然出現在這個測試目的:save_data.user = request.user 我得到的錯誤:異常值:「用戶」對象有沒有屬性「cleaned_data'`from django.db進口車型 從django.contrib.auth.models導入用戶 從django.db.models.signals導入post_save
Models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
class UserProfile(models.Model):
user = models.OneToOneField(User)
description = models.CharField(max_length=100, default='')
city = models.CharField(max_length=100, default='')
website = models.URLField(default='')
phone = models.IntegerField(default=0)
balance = models.FloatField(default=0)
bank = models.FloatField(default=0)
image = models.ImageField(upload_to='profile_image', blank=True)
def __str__(self):
return self.user.username
def create_profile(sender, **kwargs):
if kwargs['created']:
user_profile = UserProfile.objects.create(user=kwargs['instance'])
post_save.connect(create_profile, sender=User)
Forms.py
from django import forms
from django.contrib.auth.models import User
from .models import UserProfile
class ProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ('description', 'city', 'phone', 'balance', 'bank', 'website', 'image')
Views.py
from __future__ import unicode_literals
from django.core.urlresolvers import reverse
from django.contrib.auth import login, authenticate, update_session_auth_hash
from django.contrib.auth.forms import (
UserCreationForm,
UserChangeForm,
PasswordChangeForm
)
from django.contrib.auth.models import User
from django.shortcuts import render, redirect
from core.forms import (EditAccountForm, SignUpForm,
ProfileForm
)
from core.models import UserProfile
from django.views.generic import (TemplateView,ListView,
DetailView,CreateView,
UpdateView,DeleteView)
# Create your views here.
def edit_profile(request):
if request.method == 'POST':
form = ProfileForm(request.POST, instance=request.user)
if form.is_valid():
save_data = form.save()
save_data.user = request.user
print(save_data)
save_data.save()
return redirect(reverse('core:profile'))
else:
udata = UserProfile.objects.get(user=request.user)
form = ProfileForm(instance=udata)
args = {'form': form}
return render(request, 'core/test.html', args)
urls.py
from django.conf.urls import url
from django.contrib import admin
#from playme.core import views as core_views
from core import views as core_views
from django.contrib.auth import views as auth_views
# SET THE NAMESPACE!
app_name = 'core'
urlpatterns = [
url(r'^$', core_views.index, name='index'),
url(r'signup/$', core_views.signup, name='signup'),
url(r'^account/$', core_views.account, name='account'),
url(r'^profile/$', core_views.view_profile, name='profile'),
url(r'^login/$', auth_views.login, {'template_name': 'core/login.html'}, name='user_login'),
url(r'^logout/$', auth_views.logout, {'next_page': '/play/'}, name='logout'),
url(r'^edit_account/$', core_views.edit_account, name='edit_account'),
url(r'^edit_profile/$', core_views.edit_profile, name='edit_profile'),
url(r'^change_password/$', core_views.change_password, name='change_password'),
]
我打的網址是: http://127.0.0.1:8000/play/profile/ 然後去: http://127.0.0.1:8000/play/edit_profile/
的形式加載起來,但提交的時候追溯到: http://127.0.0.1:8000/play/profile/
沒有數據庫的更新。
那麼你說在POST區域:** form = ProfileForm(request.POST,instance = request.user)**應該是** form = ProfileForm(request.POST,instance = request.user.userprofile) **?因爲在** if form.is_valid()之後調試這個:**我從表單中打印了清理過的數據,並且它有正確的表單數據。 –
是的,這正是我所說的。你的調試顯示錶單數據是正確的,但那不是你的問題;如果你不給它的話,它不能保存數據到UserProfile。 –
工作過。謝謝。我仍然不完全理解你在那裏做了什麼,所以如果你有時間解釋,我會很感激。但絕不少,非常感謝。 3天后,這真是太棒了。 –