1

Having an issue with generating a specific image to appear after I have loaded it from my desktop for my users setting. When I press save, the image does not appear, nor save in my database for the specific user.

I would like for the image to appear after, clicking save, and store the generated image, in my specified path file and database. Below are some snippets of code to help in troubleshooting. Any help would be appreciated!

Thank you!

Models.py: from future import unicode_literals

import datetime
import operator
import urllib




import pytz
from imagekit.models import ProcessedImageField
from imagekit.processors import ResizeToFill

from account import signals
from account.compat import AUTH_USER_MODEL, get_user_model
from account.conf import settings
from account.fields import TimeZoneField


class Account(models.Model):

    user = models.OneToOneField(AUTH_USER_MODEL, related_name="account", verbose_name=_("user"))
    real_name = models.CharField(max_length=50,blank=True)
    birthday = models.DateField(null=True, blank=True)
    city = models.CharField(max_length=50, blank=True)
    state = models.CharField(max_length=2,blank=True)
    image_thumbnail = ProcessedImageField(upload_to='img/user_images/main',
                                           processors=[ResizeToFill(100, 50)],
                                           format='JPEG',
                                           null=True, 
                                           blank=True,
                                           options={'quality': 60})
    timezone = TimeZoneField(_("timezone"))
    language = models.CharField(_("language"),
        max_length=10,
        choices=settings.ACCOUNT_LANGUAGES,
        default=settings.LANGUAGE_CODE
    )

Forms.py

import re

try:
    from collections import OrderedDict
except ImportError:
    OrderedDict = None

from django import forms
from django.forms import extras
from django.utils.translation import ugettext_lazy as _

from django.contrib import auth

from imagekit.forms import ProcessedImageField
from imagekit.processors import ResizeToFill

from account.compat import get_user_model, get_user_lookup_kwargs
from account.conf import settings


class SettingsForm(forms.Form):

    email = forms.EmailField(label=_("Email"), required=True)
    real_name=forms.CharField(max_length=50, widget=forms.TextInput(attrs={'placeholder': 'Real Name','required':True}))
    birthday=forms.DateField(label=_(u"birthdate(mm/dd/yy)"),widget=extras.SelectDateWidget(years=range(1900, now[0]+1)),required=False)
    city=forms.CharField(max_length=30, widget=forms.TextInput(attrs={'placeholder': 'City','required':True}))
    state=forms.CharField(max_length=2, widget=forms.TextInput(attrs={'placeholder': 'State','required':True}))
    image_thumbnail = ProcessedImageField(spec_id='indieitude:account:image_thumbnail',
                                            required=False,
                                           processors=[ResizeToFill(100, 50)],
                                           format='JPEG',
                                           options={'quality': 60})

Views.py

from __future__ import unicode_literals

from django.http import Http404, HttpResponseForbidden
from django.shortcuts import redirect, get_object_or_404
from django.utils.http import base36_to_int, int_to_base36
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.views.generic.base import TemplateResponseMixin, View
from django.views.generic.edit import FormView

from django.contrib import auth, messages
from django.contrib.sites.models import get_current_site
from django.contrib.auth.tokens import default_token_generator

from account import signals
from account.compat import get_user_model
from account.conf import settings
from account.forms import SignupForm, LoginUsernameForm
from account.forms import ChangePasswordForm, PasswordResetForm, PasswordResetTokenForm
from account.forms import SettingsForm
from account.hooks import hookset
from account.mixins import LoginRequiredMixin
from account.models import SignupCode, EmailAddress, EmailConfirmation, Account, AccountDeletion
from account.utils import default_redirect
class SettingsView(LoginRequiredMixin, FormView):

    template_name = "account/settings.html"
    form_class = SettingsForm
    redirect_field_name = "next"
    messages = {
        "settings_updated": {
            "level": messages.SUCCESS,
            "text": _("Account settings updated.")
        },
    }

    def get_form_class(self):
        # @@@ django: this is a workaround to not having a dedicated method
        # to initialize self with a request in a known good state (of course
        # this only works with a FormView)
        self.primary_email_address = EmailAddress.objects.get_primary(self.request.user)
        return super(SettingsView, self).get_form_class()

    def get_initial(self):
        initial = super(SettingsView, self).get_initial()
        if self.primary_email_address:
            initial["email"] = self.primary_email_address.email
        initial["real_name"] = self.request.user.account.real_name
        initial["birthday"] = self.request.user.account.birthday
        initial["city"] = self.request.user.account.city
        initial["state"] = self.request.user.account.state
        initial["image_thumbnail"] = self.request.user.account.image_thumbnail 
        initial["timezone"] = self.request.user.account.timezone
        initial["language"] = self.request.user.account.language
        return initial
Amechi
  • 742
  • 3
  • 11
  • 32
  • Can you include your import statements? My first thoughts are that you don't need a ProcessedImageField in both your form and model. Also there are two different ProcessedImageField classes—a form field and a model field. Make sure you're using the right one in the right place. – matthewwithanm Mar 25 '14 at 11:49
  • @matthewwithanm Just added my import statements! – Amechi Mar 25 '14 at 13:31
  • Are you getting any errors? Also, is the model not saving at all, or is the image_thumbnail field just empty? One thing you should definitely change is to not use both the form field and the model field. If you want to use the form field, just use a regular ImageField in your model; if you want to use the model field, use a regular (form) ImageField in your form. – matthewwithanm Mar 25 '14 at 13:46

0 Answers0