0

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()
10
  • 1
    Please, share the code for bs4_form.html and also for MyForm Commented Aug 2, 2019 at 14:22
  • Added it right now Commented Aug 2, 2019 at 14:27
  • Your question is confusing. You don't seem to want to display it on the form, but on a separate page. And yes, writing a view and a template are exactly how you would do that; what exactly is your question? Where are you having trouble with writing those? Commented Aug 2, 2019 at 14:34
  • @DanielRoseman I did not say i want it to be on another page. This is how it works: the user submits the order form and on the same page, under 'active orders' that data submitted in the order will appear. i just don't know how to 'query' the DB to retrieve that data in django Commented Aug 2, 2019 at 14:37
  • But you're not doing anything to record an order as being associated with a user. Commented Aug 2, 2019 at 14:51

1 Answer 1

2

if I understand your question correctly, maybe you can try this:

views.py

    if request.method == 'POST':
        form = MyForm(request.POST)
        if form.is_valid():
            send = form.save(commit=False)
            send.user = request.user # set the currently logged in user
            send.save()
    else:
        form = MyForm()

    # fetch it from database then render it to the template
    active_orders = MyModel.objects.filter(user=request.user)
    context = {
       'form': form,
       'active_orders': active_orders
    }
    return render(request, "main/mytemplate.html", context)

then, in your 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>
{% for active_order in active_orders %}
<p> {{active_order.time}} - {{active_order.time}} - {{active_order.rate}} - {{active_order.user.id}}</p>
{% endfor %}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.