That's good
On Sat, Dec 1, 2018, 10:40 AM Jim Wombles <jwombles@gmail.com wrote:
--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 %}
On Friday, November 30, 2018 at 5:06:33 PM UTC-5, Jim Wombles wrote: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 = UserIt 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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.
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 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.
No comments:
Post a Comment