简体   繁体   English

如何在Django表单上显示数据库数据?

[英]How can i display database data on a Django form?

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 . 该订单已提交到SQLite数据库,并具有以下字段: 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. 提交订单后,我希望django模板在页面的“有效订单”部分下方显示它。

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?). 我不知道如何在代码方面做到这一点,但是我发现我需要创建一个对db的查询,在该数据库中将提取所有用户的订单(也许是在获取ID =用户ID的订单)。

How would i be able to perform such a operation in Django? 我将如何在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()

if I understand your question correctly, maybe you can try this: 如果我正确理解您的问题,也许您可​​以尝试以下方法:

views.py 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 %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM