我试图保存来自SignupForm的配置文件图片,但不保存该文件,我不知道Django总是采用默认值。JPG我试过了,但我做不到
你能帮我解决这个问题吗
然而,我试图保存所有的用户凭据,以尊重的UserProfile
一旦他们注册,我所有的凭据保存正确,但我不知道为什么只有profile_picture是不能从SignupForms
,而不是使用默认。在mywww.example中定义的 www.example.com
www.example.com 的应用程序
from django.core.files.base import ContentFile
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from .models import UserProfile
class SignupForm(UserCreationForm):
first_name = forms.CharField(max_length=30, required=True)
last_name = forms.CharField(max_length=30, required=True)
profile_picture = forms.ImageField(required=False, widget=forms.ClearableFileInput(attrs={'multiple': False}))
email = forms.EmailField(max_length=254, required=True)
address_line1 = forms.CharField(max_length=100, required=True)
city = forms.CharField(max_length=100, required=True)
state = forms.CharField(max_length=100, required=True)
pincode = forms.CharField(max_length=6, required=True)
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2', 'profile_picture', 'address_line1', 'city', 'state', 'pincode')
def save(self, commit=True):
user = super().save(commit=commit)
user_profile = UserProfile(user=user)
user_profile.address_line1 = self.cleaned_data['address_line1']
user_profile.city = self.cleaned_data['city']
user_profile.state = self.cleaned_data['state']
user_profile.pincode = self.cleaned_data['pincode']
if self.cleaned_data['profile_picture']:
# Save the uploaded image file with the user's username as the filename
image_file = self.cleaned_data['profile_picture']
file_extension = image_file.name.split('.')[-1]
file_name = f'{user.username}.{file_extension}'
user_profile.profile_picture.save(file_name, ContentFile(image_file.read()), save=False)
user_profile.save()
return user
这是我的 www.example.com
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
is_patient = models.BooleanField(default=False)
is_doctor = models.BooleanField(default=False)
address_line1 = models.CharField(max_length=100, null=True, blank=True)
city = models.CharField(max_length=100, null=True, blank=True)
state = models.CharField(max_length=100, null=True, blank=True)
pincode = models.CharField(max_length=6, null=True, blank=True)
profile_picture = models.ImageField(default='default.jpg', upload_to='profile_pics')
def __str__(self):
return f'{self.user.username} Profile'
我的view.py
def signup(request):
if request.method == 'POST':
form = SignupForm(request.POST, request.FILES)
if form.is_valid():
user = form.save()
profile, created = UserProfile.objects.get_or_create(user=user)
profile.address_line1 = form.cleaned_data['address_line1']
profile.city = form.cleaned_data['city']
profile.state = form.cleaned_data['state']
profile.pincode = form.cleaned_data['pincode']
if form.cleaned_data['profile_picture']:
profile.profile_picture = form.cleaned_data['profile_picture']
if request.POST.get('user_type') == 'patient':
profile.is_patient = True
else:
profile.is_doctor = True
profile.save()
messages.success(request, 'User created successfully!')
return redirect('home')
else:
form = SignupForm()
return render(request, 'signup.dj', {'form': form})
1条答案
按热度按时间3mpgtkmj1#
确保您的表单上有
enctype="multipart/form-data"
。否则,您的照片将无法工作。示例: