Home > Enterprise >  Django when I upload a image is not saved
Django when I upload a image is not saved

Time:12-09

I'm trying to create an auction system with django.

But when I create a new item for the auction, the image is not saved, but the rest of the item does. But the media folder isn't created.

SETTINGS.PY

MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

URLS.PY

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", include("core.urls")),
    path("users/", include("users.urls")),
    path("users/", include("django.contrib.auth.urls")),
    path("auction/", include("auction.urls")),
]


if settings.DEBUG:
    """
    With that Django's development server is capable of serving media files.
    """
    urlpatterns  = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

MODELS.PY

class Auction(models.Model):
    object = models.CharField(max_length=50)
    description = models.CharField(max_length=256, default="")
    image = models.ImageField(upload_to="media/", null=True, blank=True)
    open_date = models.DateTimeField(auto_now_add=True)
    close_date = models.DateTimeField()
    total_bet = models.IntegerField(default=0)
    open_price = models.FloatField(
        default=0,
    )
    close_price = models.FloatField(default=0)
    winner = models.CharField(max_length=256, default="")
    active = models.BooleanField(default=True)
    json_details_file = models.TextField(default="")
    tx = models.CharField(max_length=256, default="")

    def __str__(self):
        return self.object

FORMS.PY

class ItemForm(forms.ModelForm):
    class Meta:
        model = Auction
        fields = ["object", "description", "image", "close_date", "open_price"]
        widgets = {
            "close_date": DateTimeInput(attrs={"placeholder": "YYYY-MM-DD HH:MM"})
        }

VIEW.PY

@login_required(login_url="login")
def new_item(request):

    """
    A function that will create a new item for te auction
    """

    if request.method == "POST":
        form = ItemForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            messages.success(request, "Item create")
            return redirect("homepage")
    else:
        form = ItemForm()
    return render(request, "auction/new_item.html", {"form": form})

new_item.html

{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content %}
{% if user.is_superuser %}
    <div >
        <div >
            <h2>Creazione Item!</h2>
            <form  method="POST" novalidate>
                {% csrf_token %}
                {{ form|crispy }}
                <input type="submit"   value="Crea Item">
            </form>
        </div>
    </div>
    {% else %}
    <p>You arent' allowed</p>
    {% endif %}
{% endblock content %}

I'd looked at every line of code and tried numerous times to upload the image but still not working.

CodePudding user response:

Try adding enctype="multipart/form-data" to your html form tag like so:

<form method="post" enctype="multipart/form-data">

CodePudding user response:

In your tag use this enctype="multipart/form-data"

  • Related