我是django的新手,我在一个页面上使用了两个django模型表单但如果其中一个提交,则其他表单模型中的数据将被清除。
这是我的forms.py
Millis since start: 75
call (): 500
Millis since start: 590
call (): 1000
Millis since start: 1591
call (): 2000
Millis since start: 3593
call (): 500
Millis since start: 4094
call (): 1000
Millis since start: 5095
call (): 2000
Millis since start: 7096
call (): 500
Millis since start: 7597
call (): 1000
Millis since start: 8598
call (): 2000
这是我的views.py:
from django import forms
from .models import UserProfile
from phonenumber_field.formfields import PhoneNumberField
import floppyforms as forms
from django.contrib.auth.models import User
class ImageThumbnailFileInput(forms.ClearableFileInput):
template_name = 'floppyforms/image_thumbnail.html'
class PhoneNumberInput(forms.PhoneNumberInput):
template_name = 'floppyforms/phonenumber.html'
class ProfilePictureForm(forms.ModelForm):
"""This form will be used to edit profile picture of a user"""
class Meta:
"""Meta data about profile model is declared here"""
model = UserProfile
fields = ["picture",]
widgets = {
'picture': ImageThumbnailFileInput,
}
class ProfileForm(forms.ModelForm):
"""This form will be used to create and edit profile information of a user"""
class Meta:
"""Meta data about profile model is declared here"""
model = UserProfile
fields = ["gender", "phone_number", "address"]
widgets = {
'picture': ImageThumbnailFileInput,
'phone_number' : PhoneNumberInput,
}
first_name = forms.CharField(max_length=30, required=False)
last_name = forms.CharField(max_length=30, required=False)
email = forms.EmailField(required=False)
这是我的模板文件:
from django.shortcuts import render
from .forms import ProfileForm, ProfilePictureForm
from django.contrib.auth.models import User as ThisGuy
from django.http import HttpResponseRedirect
from .models import UserProfile
from django.core.files.uploadedfile import SimpleUploadedFile
def profilepage(request):
if request.user.is_authenticated():
#Only Open it for logged in users
profile = UserProfile.objects.filter(user__exact=request.user.pk)
picture = profile[0].picture
phone_number = profile[0].phone_number
address = profile[0].address
gender = profile[0].gender
form1 = ProfileForm(request.POST or None, initial={'phone_number': phone_number, 'address':address, 'gender': gender, 'first_name': request.user.first_name, 'last_name': request.user.last_name, 'email':request.user.email} )
form2 =ProfilePictureForm(request.POST, request.FILES, initial={'picture':picture} )
context = {
"form1": form1,
"form2": form2,
# "title": "Edit your profile"
}
else:
#Redirect to login page
return HttpResponseRedirect('/accounts/login/')
if request.method=='POST' and 'ProfileForm' in request.POST:
if form1.is_valid():
instance = form1.save(commit = False)
instance.user = request.user
requestuser = request.user
requestuser.first_name = form1.cleaned_data.get("first_name")
requestuser.last_name = form1.cleaned_data.get("last_name")
requestuser.email = form1.cleaned_data.get("email")
requestuser.save()
instance.save()
elif request.method=='POST' and 'PictureForm' in request.POST:
if form2.is_valid():
instance2 = form2.save(commit = False)
instance2.user = request.user
if request.FILES.get('picture') is None:
instance2.picture = profile[0].picture
else:
instance2.picture = request.FILES.get('picture')
instance2.save()
return render(request, "profile/edit.html", context)