The preceding "solution" solves the problem in the *site* view, but creates 
a problem in *admin*, it was to be expected. The interface of the 
*featured_image* field is lost.

This is the normal admin interface, if using 
*mezzanine.core.fields.FileField*

<https://lh3.googleusercontent.com/-NUXHDBKqQiQ/We9bAcasAOI/AAAAAAAAAfA/nNyhEYIpSS8ef8gzR3a38EYwrBgQ-g4XQCLcBGAs/s1600/Schermata%2B2017-10-24%2Balle%2B17.05.43.png>

and this is the admin interface if replace *mezzanine.core.fields.FileField* 
with *django.db.models.FileField*

<https://lh3.googleusercontent.com/-0KdmT77_zp4/We9bJcn5FQI/AAAAAAAAAfE/_quKCUlZCiAHjFs4W8xDg4UhWgq4KuEowCLcBGAs/s1600/Schermata%2B2017-10-24%2Balle%2B17.04.45.png>

Obviously, it is necessary to use *mezzanine.core.fields.FileField* if we 
want the admin interface to work properly. Then I back to the question in 
the first post: If using *mezzanine.core.fields.FileField*, why the *view* 
don't upload the images to the specified *upload_to* when runing in the 
*site*?

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.

Reply via email to