Héllo again (at last)
-- On Mon, Feb 16, 2015 at 11:20 AM Sola Chong <sola1412@gmail.com> wrote:
I having a error "expected string or buffer" when submit to upload avatar when edit profile. Can't figure out what is the problem..
I've setup a small project with your code (and small modifications) using django 1.7 and python 2.7.8 (it's important to provide that infos).
I did not reproduce the error.
I describe below what I changed or can be changed:
Model:class MyProfile(models.Model):
user = models.ForeignKey(User)
dob = models.DateField(null=True, blank=True)
address = models.CharField(max_length=100, blank=True)
contact = models.CharField(max_length=50, blank=True)
bio = models.TextField(blank=True)
avatar_url = FileField(_("Avatar"), max_length=200, format="Image", upload_to='user/avatar', blank=True)
banner_url = FileField(_("Cover"), max_length=200, format="Image", upload_to='user/cover', blank=True)
created = models.DateTimeField(auto_now_add=True)
last_modified = models.DateTimeField(auto_now=True)
- There is no "format" parameter in FileField constructor.
- it's better to stay consistent regarding the model fields and use models.FileField instead of FileField alone. If FileField is a custom class, it should have another name.
- User model is referenced directly. You can live with that. It can break in some situations cf. https://docs.djangoproject.com/en/1.7/topics/auth/customizing/#referencing-the-user-model
Forms:class UserModelForm(forms.ModelForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'email')
widgets = {
'email': forms.TextInput(attrs={'readonly':'readonly'}),
}
class UserProfileModelForm(forms.ModelForm):
dob = forms.DateField(widget=forms.DateInput(format = '%d/%m/%Y'),
input_formats=('%d/%m/%Y',),
required=True, help_text='dd/mm/yyyy')
avatar_url = forms.FileField()
class Meta:
model = MyProfile
fields = ('dob', 'address', 'contact', 'bio', 'avatar_url')
Views:def editProfileView(request, template="controlpanel/cp_edit_profile.html"):
myprofiles = MyProfile.objects.all()
myprofile = get_object_or_404(myprofiles, user=request.user)
if request.method == "POST":
form = UserProfileModelForm(request.POST, request.FILES, instance=myprofile)
userForm = UserModelForm(request.POST, request.FILES, instance=request.user)
if form.is_valid() and userForm.is_valid():
user = userForm.save()
profile = form.save(commit = False)
profile.user = user
profile.save()
info(request, _("Successfully Edit"))
else:
userForm = UserModelForm(instance=request.user)
form = UserProfileModelForm(instance=myprofile)
context = {"cpprofile": myprofile, 'userForm':userForm, 'form':form}
return render(request, template, context)Template:<form method="post" action="/editprofile" enctype="multipart/form-data">
<fieldset>
<legend>{{ title }}</legend>
{% fields_for userForm %}
{% fields_for form %}
<div class="form-actions">
{% block account_form_actions %}
<input class="btn btn-primary btn-lg pull-right" type="submit" value="Submit">
{% endblock %}
</div>
</fieldset>
</form>
I don't know {% fields_for %} tag. I used {{ userForm.as_ul }} instead.
If you are interested, I can push the code online.
HTH
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAL7_Mo99KPV%2BACtMEcmcUanVTMiY5idkouAsDrmv-UdWVM1M7w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment