用例是登录使用,可以将他们的个人资料调到表格中,编辑它并在数据库中更新。
使用下面的代码可以查看正确的配置文件数据。它也可以被称为可编辑的形式。表单允许编辑数据,但在提交时,数据库不会更新,并显示原始配置文件。
我已经尝试了所有内容,包括控制台打印语句,所以我知道它正在进入代码的POST部分,是一个有效的表单并执行初始save()。
任何见解都将受到赞赏(我正在使用Django 1.11)。我不想开始运行兔子,但我唯一没有尝试的是打破OneToOne与User的关系并使用foriegnkey因为我希望在用户注册时创建配置文件条目。
备注: 当我将其打开以进行测试时:save_data.user = request.user 我收到错误:异常值:'用户'对象没有来自django.db导入模型的属性'cleaning_data'` 来自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/
没有数据库更新。
答案 0 :(得分:1)
我很惊讶这不会给出错误。
当您在GET上实例化表单时,您正确地将UserProfile对象作为if (colorsApproximatelyEqual(color, FIELD_GREEN, 0.1)) {
// process click on field...
}
参数传递 - 这是正确的,因为表单将其作为模型。但是当您在POST上实例化时,您错误地传递了User对象 - instance
。您应该像在其他分支中那样传递配置文件。
请注意,在这两种情况下,只需执行request.user
即可更简单地获取个人资料。
答案 1 :(得分:1)
表格无效的情况根本没有在这里处理。您的代码应如下所示
def edit_profile(request):
if request.method == 'POST':
form = ProfileForm(request.POST, instance=request.user)
if form.is_valid():
save_data = form.save(commit=False)
save_data.user = request.user
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)
请注意缩进的更改。