I solved the problem using *django.db.models.FileField*, instead of its
heir *mezzanine.core.fields.FileField*. I have also had to remove the
*format* argument, since *models.FileField* does not expect it. Then I had
to specify the *accept* attribute in the *widgets* dictionary.
*in a models.py*
class Recipe(Displayable, AdminThumbMixin):
featured_image = models.FileField(upload_to=upload_to(
"recipes.Recipe.featured_image", "recipes"), max_length=255)
in forms.py
class CreateRecipeForm(forms.ModelForm):
class Meta:
model = Recipe
widgets = {"featured_image": forms.FileInput(attrs={"accept":
"image/jpeg"})}
Il giorno lunedì 23 ottobre 2017 22:51:01 UTC+2, Rainell Dilou Gómez ha
scritto:
>
> Hello everyone,
> if I create a new instance of a custom content type from the admin
> everything work fine, but from the site the view try to upload the image to
> a wrong path, and image is not uploaded. The value saved in the
> featured_image field in the DB (/static/media/image_name.jpg) does not
> correspond with the value set by me (upload_to) in the model.
> This is a summary of the code of the files involved. Is there something
> I'm doing wrong?
>
> *in models.py*
>
> class Recipe(Displayable, AdminThumbMixin):
>
> featured_image = FileField(upload_to=upload_to(
> "recipes.Recipe.featured_image", "recipes"),
> format="Image", max_length=255)
> admin_thumb_field = "featured_image"
>
> # other fields, Meta, etc.
>
>
> *in forms.py*
>
> class CreateRecipeForm(forms.ModelForm):
> class Meta:
> model = Recipe
> fields = ["featured_image",]
> widgets = {"featured_image": forms.FileInput()}
>
>
> *in views.py*
>
> def create_recipe(request, template="recipes/create_recipe.html",
> extra_context=None):
>
> if request.method == "POST":
> form = CreateRecipeForm(request.POST, request.FILES)
> if form.is_valid():
> form.save()
> else:
> form = CreateRecipeForm()
>
> context = {"form": form}
> context.update(extra_context or {})
> return TemplateResponse(request, [template], context)
>
>
> *in a template*
>
> {% extends "base.html" %}
> {% load i18n mezzanine_tags %}
>
> {% block extra_head %}
> {{ block.super }}{{ form.media }}{% endif %}
> {% endblock %}
>
> {% block main %}
> <form method="post" enctype="multipart/form-data">
> {% fields_for form %}
> <button type="submit">{% trans "Submit" %}</button>
> </form>
> {% endblock %}
>
>
>
--
You received this message because you are subscribed to the Google Groups
"Mezzanine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.