Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialStuart McIntosh
Python Web Development Techdegree Graduate 22,874 PointsHelp with Django Request.Files and form fields
Hi there, I am reqlly stuck on 2 subjects:
I have sucessfully created a view and form to upload an image, however, when I want to edit that image request.FILES is empty and I get an error on the form
I have a EditProfileForm where I want to allow the user to change some fieldd. I remove the date_of_birth field from the class meta, however, in the form it still says its required.
Any help would be welcome
Stuart
Views.py
@login_required
def edit_profile(request):
''' edit profile view for the accounts application '''
user = get_object_or_404(User, username=request.user)
form = EditProfileForm(instance=user)
print('Files : {}'.format(request.FILES))
if request.method == 'POST':
form = EditProfileForm(instance=user,
data=request.POST,
files=request.FILES
)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('accounts:view_profile'))
else:
print('error occurred on form')
return render(request, 'accounts/edit_profile.html', (
{'form': form, 'user':user, 'profile':user.userprofile}))
forms.py
class EditProfileForm(UserChangeForm):
''' extends the UserChangeForm to allow changes to the UserProfile '''
date_of_birth = forms.DateTimeField(label='Date of Birth',
input_formats=['%Y-%m-%d',
'%m/%d/%Y',
'%m/%d/%y'],
)
bio = forms.CharField(max_length=300)
avatar = forms.ImageField()
class Meta:
model = User
fields = (
'username',
'last_name',
'email',
'bio',
'avatar',
)
def clean_password(self):
return self.cleaned_data['password']
def save(self, commit=True):
if not commit:
raise NotImplementedError("Can't create User and UserProfile without database save")
user = super(EditProfileForm, self).save(commit=False)
user.password = self.clean_password()
user_profile = UserProfile(user=user,
bio=self.cleaned_data['bio'],
avatar=self.cleaned_data['avatar'])
user.save()
user_profile.save()
return user, user_profile
template
{% extends "layout.html" %}
{% block title %}Edit Profile | {{ super }}{% endblock %}
{% block body %}
<form enctype="multipart/form-data" method="POST" action="/accounts/edit_profile/">
{% csrf_token %}
<p><label for="id_username">Username:</label> <input type="text" name="username" value={{ user.username }} maxlength="150" id="id_username" required /> <span class="helptext">Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.</span></p>
<p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" value={{ user.last_name }} maxlength="30" id="id_last_name" /></p>
<p><label for="id_email">Email address:</label> <input type="email" name="email" value={{ user.email }} maxlength="254" id="id_email" /></p>
<p><label for="id_bio">Bio:</label> <input type="text" name="bio" value={{ user.userprofile.bio }} maxlength="300" id="id_bio" required /></p>
<p><label for="id_avatar">Avatar:</label> Currently: <a href="{{ user.userprofile.avatar.url }}">{{ user.userprofile.avatar }}</a><br />
Change:
<input type="file" name="avatar" id="id_avatar" /></p>
<input type="submit" class="button-primary" value="Save Changes">
</form>
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-danger">
<strong>{{ error|escape }} {{ field }}{{ field.label }}</strong>
</div>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<div class="alert alert-danger">
<strong>{{ error|escape }} {{ field }} {{ field.label }}</strong>
</div>
{% endfor %}
{% endif %}
{% endblock %}