I created a page where the user submits an order. The order is submitted to the SQLite DB and has the following fields: Time - Status - Rate - UserID. Once the order is submitted, i would like my django template to show it below an 'Active orders' part of the page.
I don't know how to do it in terms of code, but i figured out that i need to create a query to db where all the user's `orders are fetched (maybe fetching the orders where ID = user's id?).
How would i be able to perform such a operation in Django? In my views?
Here is the template's view:
def myview(request):
item = get_object_or_404(market, item=item)
if request.method == 'POST':
form = MyForm(request.POST)
if form.is_valid():
send = form.save()
send.save()
messages.success(request, f"Success")
else:
form = MyForm()
return render(request,
"main/mytemplate.html",
context={"form":form})
And part of the template:
<form method="post" novalidate>
{% csrf_token %}
{% include 'main/includes/bs4_form.html' with form=form1 %}
<button name="button1" type="submit" class="btn btn-primary">BUY</button>
</form>
<h3> ACTIVE ORDERS </h3>
<p> Here should go the orders... </p>
Form:
class MyForm(forms.ModelForm):
status = forms.CharField()
rate = forms.FloatField()
class Meta:
model = MyModel
fields = ("status", "rate")
def save(self, commit=True):
send = super(MyForm, self).save(commit=False)
if commit:
send.save()
return send
Model:
class MyModel(models.Model):
rate = models.FloatField()
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True, editable=False)
time = models.DateTimeField(auto_now_add=True)
status = models.CharField(max_length=50)
def save(self): # ALL the signature
super(MyModel, self).save()
bs4_form.htmland also forMyForm