0

I am working on CS50W project 2 - commerce and I've been working on the Create Listing point. I have a route /create where I have to get data and save it in models. I'll leave the code snippets below . I tried to use the ModelForm class , then validate the data and save() but it doesn't save data . I also ran queries in shell on the models to see if data is there but nothing. In the code snippets I'll leave only the useful parts to look cleaner. I checked and the imports ( importing models to views , other modules , etc) are fine. I think I made a mistake somewhere else , but I just can't see it. How to solve this?

the ModelForm

class AuctionsForm(forms.ModelForm):
class Meta:
    model = Auctions
    fields = ['title' , 'description' , 'category' , 'image']

views.py

@login_required
def create(request):
if request.method == "POST":
    form = AuctionsForm(request.POST)
    
    if form.is_valid():
        form.save()
    else: 
        form = AuctionsForm()


return render(request, "auctions/create.html" , {"form" : AuctionsForm()} )

models.py

class Auctions(models.Model):
CATEGORY = [
    ("No Category" , "No Category"),
    ("Fashion" , "Fashion"),
    ("Home" , "Home"),
    ("Toys" , "Toys"),
    ("Technology" , "Technology"),

]
title = models.CharField(max_length= 50)
description = models.TextField(max_length=150)
category = models.CharField(max_length = 40 , choices= CATEGORY , default="None")
image = models.URLField(default="" , blank=True)
is_active = models.BooleanField(default=True)
date = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey('auctions.User' , on_delete=models.CASCADE , related_name='user_auctions')
watchlist = models.ForeignKey('auctions.User' , on_delete=models.CASCADE , related_name='watchlist_auctions')

def __str__(self):
    return self.title

create.html

{% extends "auctions/layout.html" %}

{% block body %}

<h1>Create Listing Here</h1>

<form method="POST" action="{% url 'index' %}">
  {% csrf_token %}

  {{form}}
  
  <input type="submit" value="Create Listing">
</form>

{% endblock %}

index.html

{% extends "auctions/layout.html" %}

{% block body %}
<h2>Active Listings</h2>
{% for i in show%}
    <p>{{i.title}}</p>
    <p>{{i.description}}</p>
{% endfor%}
{% endblock %}
alxghg
  • 11
  • 2

1 Answers1

0

forms.py

class AuctionsForm(forms.ModelForm):
    class Meta:
       model = models.Auction
       fields = ['title', 'description', 'category', 'image']

views.py

def create_auction(request):
    
    if request.method == 'POST':
        form = AuctionsForm(request.POST)
    
        if form.is_valid():
            auction = form.save(commit=False)
            # do something if you but in the end
            auction.save()

            # do want you want here, or return in to creation page dunno
            return render(request, "success?idk/", {"auction": auction})
        else:
            # maybe you want to render the errors?
            # https://stackoverflow.com/questions/14647723/django-forms-if-not-valid-show-form-with-error-message
            return render(request, "auctions/create.html", {"form": form})
   
    form = AuctionForm()
    return render(request, "auction/create.html", {"form":form})

# maybe you had listing view here

use Auction instead of Auctions, Django pluralize(s) it

k13lhyre
  • 1
  • 1
  • 1