Discussion:
Why isn't user.photo being saved in UpdateView?
(too old to reply)
Jim Wombles
2018-11-30 21:21:26 UTC
Permalink
For some reason, the updateview will save all of the other user fields (and
update them if changed) to the database. Do I need to handle the
user.photo field differently?

# views.py
class UserUpdateView(LoginRequiredMixin, UpdateView):

model = User
fields = ["name", "photo", "city", "experience", "domain", "bio"]

def get_success_url(self):
return reverse("users:detail", kwargs={"username": self.request.user
.username})

def get_object(self):
return User.objects.get(username=self.request.user.username)

user_update_view = UserUpdateView.as_view()


The form:
# form.py
from django.contrib.auth import get_user_model, forms
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _


User = get_user_model()

class UserChangeForm(forms.UserChangeForm):

class Meta(forms.UserChangeForm.Meta):
model = User




It does save the photo though in the Admin.
# admin.py
from django.contrib import admin
from django.contrib.auth import admin as auth_admin
from django.contrib.auth import get_user_model

from lexpy.users.forms import UserChangeForm, UserCreationForm

User = get_user_model()

@admin.register(User)
class UserAdmin(auth_admin.UserAdmin):


form = UserChangeForm
add_form = UserCreationForm
fieldsets = (("User", {"fields": ("name", "photo", "height_field",
"width_field",
"city", "experience", "domain", "bio")}),) + auth_admin.
UserAdmin.fieldsets
list_display = ["username", "name", "is_superuser"]
search_fields = ["name"]
Enter code here...


My Models:

# models.py
from datetime import date


from django.contrib.auth.models import AbstractUser
from django.db import models
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _


def user_directory_path(instance, filename):
#file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
return 'user_{0}/{1}'.format(instance.username, filename)


class User(AbstractUser):
EXPERIENCE_BEGINNER = 'B'
EXPERIENCE_BEGINT = 'BI'
EXPERIENCE_INTERMEDIATE = 'I'
EXPERIENCE_INTERVANCED = 'IA'
EXPERIENCE_ADVANCED = 'A'
DOMAIN_WEB_DEVELOPMENT = 'WD'
DOMAIN_SCIENTIFIC_AND_NUMERIC = 'SN'
DOMAIN_EDUCATION = 'E'
DOMAIN_DESKTOP_GUIS = 'DG'
DOMAIN_GAME_DEVELOPMENT = 'GD'
DOMAIN_SOFTWARE_DEVELOPMENT = 'SD'
DOMAIN_SCRIPTING = 'S'


EXPERIENCE_CHOICES = (
(EXPERIENCE_BEGINNER, 'Beginner'),
(EXPERIENCE_BEGINT, 'Beginner to Intermediate'),
(EXPERIENCE_INTERMEDIATE, 'Intermediate'),
(EXPERIENCE_INTERVANCED, 'Intermediate to Advanced'),
(EXPERIENCE_ADVANCED, 'Advanced / Professional'),
)


DOMAIN_CHOICES = (
(DOMAIN_WEB_DEVELOPMENT, "Web Applications"),
(DOMAIN_SCIENTIFIC_AND_NUMERIC, "Scientific or Numeric Applications"
),
(DOMAIN_EDUCATION, "Education"),
(DOMAIN_DESKTOP_GUIS, "Desktop Software (GUI's)"),
(DOMAIN_GAME_DEVELOPMENT, "Game Development"),
(DOMAIN_SOFTWARE_DEVELOPMENT, "Support Software"),
(DOMAIN_SCRIPTING, "Scripting"),
)


# First Name and Last Name do not cover name patterns
# around the globe.
name = models.CharField(_("Name of User"), blank=True, max_length=255)
photo = models.ImageField(upload_to=user_directory_path,
blank=True,
width_field="width_field",
height_field="height_field")
height_field = models.IntegerField(default=0)
width_field = models.IntegerField(default=0)
city = models.CharField(max_length=50, blank=True)
# Fix default
experience = models.CharField(max_length=2, choices=EXPERIENCE_CHOICES,
blank=True, null=True)
domain = models.CharField(max_length=2, choices=DOMAIN_CHOICES, blank=
True, null=True)
bio = models.TextField(max_length=500, blank=True)


def get_absolute_url(self):
return reverse("users:detail", kwargs={"username": self.username})
--
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+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9a0c73a2-3e4c-41b8-b905-bb75639a16cf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jim Wombles
2018-12-01 09:40:43 UTC
Permalink
I solved the problem after digging around in the Django Forms related to
ImageFields and FileFields. I needed to add the <form
enctype="multipart/form-data"> attribute in order for Django to process
forms that contain ImageFields and/or FileFields.
#user_form.html

{% extends "base.html" %}
{% load crispy_forms_tags %}


{% block title %}{{ user.username }}{% endblock %}


{% block content %}
<div class="container">
<h1>{{ user.username }}</h1>
<form enctype="multipart/form-data" class="form-horizontal" method="post"
action="{% url 'users:update' %}">
{% csrf_token %}
{{ form|crispy }}
<div class="control-group">
<div class="controls">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</div>
</form>
</div>
{% endblock %}
Post by Jim Wombles
For some reason, the updateview will save all of the other user fields
(and update them if changed) to the database. Do I need to handle the
user.photo field differently?
# views.py
model = User
fields = ["name", "photo", "city", "experience", "domain", "bio"]
return reverse("users:detail", kwargs={"username": self.request.
user.username})
return User.objects.get(username=self.request.user.username)
user_update_view = UserUpdateView.as_view()
# form.py
from django.contrib.auth import get_user_model, forms
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
User = get_user_model()
model = User
It does save the photo though in the Admin.
# admin.py
from django.contrib import admin
from django.contrib.auth import admin as auth_admin
from django.contrib.auth import get_user_model
from lexpy.users.forms import UserChangeForm, UserCreationForm
User = get_user_model()
@admin.register(User)
form = UserChangeForm
add_form = UserCreationForm
fieldsets = (("User", {"fields": ("name", "photo", "height_field",
"width_field",
"city", "experience", "domain", "bio")}),) + auth_admin.
UserAdmin.fieldsets
list_display = ["username", "name", "is_superuser"]
search_fields = ["name"]
Enter code here...
# models.py
from datetime import date
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _
#file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
return 'user_{0}/{1}'.format(instance.username, filename)
EXPERIENCE_BEGINNER = 'B'
EXPERIENCE_BEGINT = 'BI'
EXPERIENCE_INTERMEDIATE = 'I'
EXPERIENCE_INTERVANCED = 'IA'
EXPERIENCE_ADVANCED = 'A'
DOMAIN_WEB_DEVELOPMENT = 'WD'
DOMAIN_SCIENTIFIC_AND_NUMERIC = 'SN'
DOMAIN_EDUCATION = 'E'
DOMAIN_DESKTOP_GUIS = 'DG'
DOMAIN_GAME_DEVELOPMENT = 'GD'
DOMAIN_SOFTWARE_DEVELOPMENT = 'SD'
DOMAIN_SCRIPTING = 'S'
EXPERIENCE_CHOICES = (
(EXPERIENCE_BEGINNER, 'Beginner'),
(EXPERIENCE_BEGINT, 'Beginner to Intermediate'),
(EXPERIENCE_INTERMEDIATE, 'Intermediate'),
(EXPERIENCE_INTERVANCED, 'Intermediate to Advanced'),
(EXPERIENCE_ADVANCED, 'Advanced / Professional'),
)
DOMAIN_CHOICES = (
(DOMAIN_WEB_DEVELOPMENT, "Web Applications"),
(DOMAIN_SCIENTIFIC_AND_NUMERIC, "Scientific or Numeric
Applications"),
(DOMAIN_EDUCATION, "Education"),
(DOMAIN_DESKTOP_GUIS, "Desktop Software (GUI's)"),
(DOMAIN_GAME_DEVELOPMENT, "Game Development"),
(DOMAIN_SOFTWARE_DEVELOPMENT, "Support Software"),
(DOMAIN_SCRIPTING, "Scripting"),
)
# First Name and Last Name do not cover name patterns
# around the globe.
name = models.CharField(_("Name of User"), blank=True, max_length=255)
photo = models.ImageField(upload_to=user_directory_path,
blank=True,
width_field="width_field",
height_field="height_field")
height_field = models.IntegerField(default=0)
width_field = models.IntegerField(default=0)
city = models.CharField(max_length=50, blank=True)
# Fix default
experience = models.CharField(max_length=2, choices=EXPERIENCE_CHOICES
, blank=True, null=True)
domain = models.CharField(max_length=2, choices=DOMAIN_CHOICES, blank=
True, null=True)
bio = models.TextField(max_length=500, blank=True)
return reverse("users:detail", kwargs={"username": self.username})
--
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+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ccffc13c-fd73-465f-985a-f8cd72315c59%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jani Tiainen
2018-12-01 10:00:13 UTC
Permalink
It's not a Django but generic requirement to send files within HTML form.
Post by Jim Wombles
I solved the problem after digging around in the Django Forms related to
ImageFields and FileFields. I needed to add the <form
enctype="multipart/form-data"> attribute in order for Django to process
forms that contain ImageFields and/or FileFields.
#user_form.html
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block title %}{{ user.username }}{% endblock %}
{% block content %}
<div class="container">
<h1>{{ user.username }}</h1>
<form enctype="multipart/form-data" class="form-horizontal" method=
"post" action="{% url 'users:update' %}">
{% csrf_token %}
{{ form|crispy }}
<div class="control-group">
<div class="controls">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</div>
</form>
</div>
{% endblock %}
Post by Jim Wombles
For some reason, the updateview will save all of the other user fields
(and update them if changed) to the database. Do I need to handle the
user.photo field differently?
# views.py
model = User
fields = ["name", "photo", "city", "experience", "domain", "bio"]
return reverse("users:detail", kwargs={"username": self.request.
user.username})
return User.objects.get(username=self.request.user.username)
user_update_view = UserUpdateView.as_view()
# form.py
from django.contrib.auth import get_user_model, forms
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
User = get_user_model()
model = User
It does save the photo though in the Admin.
# admin.py
from django.contrib import admin
from django.contrib.auth import admin as auth_admin
from django.contrib.auth import get_user_model
from lexpy.users.forms import UserChangeForm, UserCreationForm
User = get_user_model()
@admin.register(User)
form = UserChangeForm
add_form = UserCreationForm
fieldsets = (("User", {"fields": ("name", "photo", "height_field",
"width_field",
"city", "experience", "domain", "bio")}),) + auth_admin.
UserAdmin.fieldsets
list_display = ["username", "name", "is_superuser"]
search_fields = ["name"]
Enter code here...
# models.py
from datetime import date
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _
#file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
return 'user_{0}/{1}'.format(instance.username, filename)
EXPERIENCE_BEGINNER = 'B'
EXPERIENCE_BEGINT = 'BI'
EXPERIENCE_INTERMEDIATE = 'I'
EXPERIENCE_INTERVANCED = 'IA'
EXPERIENCE_ADVANCED = 'A'
DOMAIN_WEB_DEVELOPMENT = 'WD'
DOMAIN_SCIENTIFIC_AND_NUMERIC = 'SN'
DOMAIN_EDUCATION = 'E'
DOMAIN_DESKTOP_GUIS = 'DG'
DOMAIN_GAME_DEVELOPMENT = 'GD'
DOMAIN_SOFTWARE_DEVELOPMENT = 'SD'
DOMAIN_SCRIPTING = 'S'
EXPERIENCE_CHOICES = (
(EXPERIENCE_BEGINNER, 'Beginner'),
(EXPERIENCE_BEGINT, 'Beginner to Intermediate'),
(EXPERIENCE_INTERMEDIATE, 'Intermediate'),
(EXPERIENCE_INTERVANCED, 'Intermediate to Advanced'),
(EXPERIENCE_ADVANCED, 'Advanced / Professional'),
)
DOMAIN_CHOICES = (
(DOMAIN_WEB_DEVELOPMENT, "Web Applications"),
(DOMAIN_SCIENTIFIC_AND_NUMERIC, "Scientific or Numeric
Applications"),
(DOMAIN_EDUCATION, "Education"),
(DOMAIN_DESKTOP_GUIS, "Desktop Software (GUI's)"),
(DOMAIN_GAME_DEVELOPMENT, "Game Development"),
(DOMAIN_SOFTWARE_DEVELOPMENT, "Support Software"),
(DOMAIN_SCRIPTING, "Scripting"),
)
# First Name and Last Name do not cover name patterns
# around the globe.
name = models.CharField(_("Name of User"), blank=True, max_length=255
)
photo = models.ImageField(upload_to=user_directory_path,
blank=True,
width_field="width_field",
height_field="height_field")
height_field = models.IntegerField(default=0)
width_field = models.IntegerField(default=0)
city = models.CharField(max_length=50, blank=True)
# Fix default
experience = models.CharField(max_length=2, choices=
EXPERIENCE_CHOICES, blank=True, null=True)
domain = models.CharField(max_length=2, choices=DOMAIN_CHOICES, blank
=True, null=True)
bio = models.TextField(max_length=500, blank=True)
return reverse("users:detail", kwargs={"username": self.username
})
--
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
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/ccffc13c-fd73-465f-985a-f8cd72315c59%40googlegroups.com
<https://groups.google.com/d/msgid/django-users/ccffc13c-fd73-465f-985a-f8cd72315c59%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHn91oeE7fsDdDtRtDvM%2BH6vSv2ZFYnF2pfAo5HbB%3DjqafdrAw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Abdul Hayatu
2018-12-01 10:14:15 UTC
Permalink
That's good
Post by Jim Wombles
I solved the problem after digging around in the Django Forms related to
ImageFields and FileFields. I needed to add the <form
enctype="multipart/form-data"> attribute in order for Django to process
forms that contain ImageFields and/or FileFields.
#user_form.html
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block title %}{{ user.username }}{% endblock %}
{% block content %}
<div class="container">
<h1>{{ user.username }}</h1>
<form enctype="multipart/form-data" class="form-horizontal" method=
"post" action="{% url 'users:update' %}">
{% csrf_token %}
{{ form|crispy }}
<div class="control-group">
<div class="controls">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</div>
</form>
</div>
{% endblock %}
Post by Jim Wombles
For some reason, the updateview will save all of the other user fields
(and update them if changed) to the database. Do I need to handle the
user.photo field differently?
# views.py
model = User
fields = ["name", "photo", "city", "experience", "domain", "bio"]
return reverse("users:detail", kwargs={"username": self.request.
user.username})
return User.objects.get(username=self.request.user.username)
user_update_view = UserUpdateView.as_view()
# form.py
from django.contrib.auth import get_user_model, forms
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
User = get_user_model()
model = User
It does save the photo though in the Admin.
# admin.py
from django.contrib import admin
from django.contrib.auth import admin as auth_admin
from django.contrib.auth import get_user_model
from lexpy.users.forms import UserChangeForm, UserCreationForm
User = get_user_model()
@admin.register(User)
form = UserChangeForm
add_form = UserCreationForm
fieldsets = (("User", {"fields": ("name", "photo", "height_field",
"width_field",
"city", "experience", "domain", "bio")}),) + auth_admin.
UserAdmin.fieldsets
list_display = ["username", "name", "is_superuser"]
search_fields = ["name"]
Enter code here...
# models.py
from datetime import date
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _
#file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
return 'user_{0}/{1}'.format(instance.username, filename)
EXPERIENCE_BEGINNER = 'B'
EXPERIENCE_BEGINT = 'BI'
EXPERIENCE_INTERMEDIATE = 'I'
EXPERIENCE_INTERVANCED = 'IA'
EXPERIENCE_ADVANCED = 'A'
DOMAIN_WEB_DEVELOPMENT = 'WD'
DOMAIN_SCIENTIFIC_AND_NUMERIC = 'SN'
DOMAIN_EDUCATION = 'E'
DOMAIN_DESKTOP_GUIS = 'DG'
DOMAIN_GAME_DEVELOPMENT = 'GD'
DOMAIN_SOFTWARE_DEVELOPMENT = 'SD'
DOMAIN_SCRIPTING = 'S'
EXPERIENCE_CHOICES = (
(EXPERIENCE_BEGINNER, 'Beginner'),
(EXPERIENCE_BEGINT, 'Beginner to Intermediate'),
(EXPERIENCE_INTERMEDIATE, 'Intermediate'),
(EXPERIENCE_INTERVANCED, 'Intermediate to Advanced'),
(EXPERIENCE_ADVANCED, 'Advanced / Professional'),
)
DOMAIN_CHOICES = (
(DOMAIN_WEB_DEVELOPMENT, "Web Applications"),
(DOMAIN_SCIENTIFIC_AND_NUMERIC, "Scientific or Numeric
Applications"),
(DOMAIN_EDUCATION, "Education"),
(DOMAIN_DESKTOP_GUIS, "Desktop Software (GUI's)"),
(DOMAIN_GAME_DEVELOPMENT, "Game Development"),
(DOMAIN_SOFTWARE_DEVELOPMENT, "Support Software"),
(DOMAIN_SCRIPTING, "Scripting"),
)
# First Name and Last Name do not cover name patterns
# around the globe.
name = models.CharField(_("Name of User"), blank=True, max_length=255
)
photo = models.ImageField(upload_to=user_directory_path,
blank=True,
width_field="width_field",
height_field="height_field")
height_field = models.IntegerField(default=0)
width_field = models.IntegerField(default=0)
city = models.CharField(max_length=50, blank=True)
# Fix default
experience = models.CharField(max_length=2, choices=
EXPERIENCE_CHOICES, blank=True, null=True)
domain = models.CharField(max_length=2, choices=DOMAIN_CHOICES, blank
=True, null=True)
bio = models.TextField(max_length=500, blank=True)
return reverse("users:detail", kwargs={"username": self.username
})
--
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
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/ccffc13c-fd73-465f-985a-f8cd72315c59%40googlegroups.com
<https://groups.google.com/d/msgid/django-users/ccffc13c-fd73-465f-985a-f8cd72315c59%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMD8rLTgsbXwALq3A9Vx0QfQOHSxJY21cHE%2B18QgCmh8nOyLTg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...