0

I have a form that asks for various inputs one of which is a file input. I want the uploaded files to be stored inside /media/images which is the case when I upload files from the admin console since I specified the upload location in models.py. How can I do the same and store the files that are uploaded via HTML forms in /media/images instead of /media?

games.html:

{% block body %}
<div id="new_game_form" style="display:none; margin: 20px;">
  <form class="row g-3" action="{% url 'new_game'%}" method="post">
    {% csrf_token %}
      <div class="col-md-6">
        <label class="form-label"><strong>Title</strong></label>
        <input type="text" class="form-control" id="title" name="title">
      </div>
      <div class="col-md-6">
        <label class="form-label"><strong>Sub Title</strong></label>
        <input type="text" class="form-control" id="sub_title" name="sub_title">
      </div>
      <div class="col-12">
        <label class="form-label"><strong>Description</strong></label>
        <input type="textarea" class="form-control" id="description" name="description">
      </div>
      <div class="col-12">
        <label class="form-label"><strong>Thumbnail</strong></label>
        <input type="file" class="form-control" id="thumbnail" name="thumbnail">
      </div>
      <div class="col-12">
        <label class="form-label"><strong>Platform</strong></label>
        <input type="text" class="form-control" id="platform" name="platform">
      </div>
      <div class="col-12">
        <label class="form-label"><strong>Game Link</strong></label>
        <input type="url" class="form-control" id="game_link" name="game_link">
      </div>
      <div class="col-12">
        <label class="form-label"><strong>Launch Date</strong></label>
        <input type="date" class="form-control" id="launch_date" name="launch_date">
      </div>
      <div class="col-12">
        <input type="submit" class="btn btn-dark" id="submit" value="Submit" style="margin-top: 10px;">
      </div>
    </form>
</div>

views.py:

def new_game(request):
    if request.method == "POST":
        title = request.POST["title"]
        sub_title = request.POST["sub_title"]
        description = request.POST["description"]
        image = request.POST["thumbnail"]
        platform = request.POST["platform"]
        game_link = request.POST["game_link"]
        launch_date = request.POST["launch_date"]

        submitted_game = Game.objects.create(
            title = title,
            sub_title = sub_title,
            description = description,
            image = image,
            platform = platform,
            link = game_link,
            launch_date = launch_date
            )
        submitted_game.save()
        return HttpResponseRedirect(reverse("games"))
    return render(request, "potbs/games.html")

models.py:

class Game(models.Model):
  title = models.CharField(max_length = 64)
  sub_title = models.TextField(blank = True)
  description = models.TextField(blank = True)
  image = models.ImageField(upload_to = "images", blank = True)
  platform = models.CharField(max_length = 100)
  link = models.URLField(max_length = 200, blank = True)
  launch_date = models.DateField(auto_now = False, auto_now_add = False, null = True)
  created_date = models.DateTimeField(auto_now_add = True)
Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
  • `upload_to` should be enough. Please show settings.py part related to media files and file storage. Also note that `Game.objects.create` = `instantiate object + save to database` and by calling `.save()` afterwards you save to database one more time thus two records will be saved. – Ivan Starostin Apr 09 '23 at 14:44

0 Answers0