0

I am a newbie in Django. I want to show the food_status in drop-down list options, therefore the chef can select one of them, change it, and update it into database. It can be updated into database, but i am not sure how to display the drop-down list on template based on the food_status that I have in models.py.

Anyone know how to do it?

models.py

class OrderItem(models.Model):
    Table_No = models.IntegerField(blank=False)
    FoodId = models.TextField()
    Item = models.TextField()
    Qty = models.DecimalField(max_digits=5, decimal_places=0)
    Price = models.DecimalField(max_digits=10, decimal_places=2)
    TotalPrice = models.TextField()
    Note = models.TextField(max_length=100, null=True)
    OrderId = models.TextField(max_length=5, null=True)

    FoodStatus = (
        ('1', 'Has been ordered'),
        ('2', 'cooked'),
        ('3', 'ready to be served'),
        ('4', 'done'),
    )
    food_status = models.CharField(max_length=50, choices=FoodStatus)

views.py

def see_order(request):
if request.method == "POST":
    OrderId = request.POST.get("OrderId")                           
    customerOrder = OrderItem(OrderId=OrderId)  
    so = OrderItem.objects.filter(OrderId=OrderId)  
    return render(request, 'restaurants/see_order.html', {'so': so}) 
else:
    return render(request, 'restaurants/customer_page.html')  

see_order.html

<form action="#" method="post">
<style>
table, th, td {
    border: 1px solid black;
    table-layout: fixed ;
    height: "2000" ;
    width: "2000" ;
}
</style>
    {% csrf_token %}                                
    {% for order in so %}                           
    <table>                                     
        <tr>
            <th>Table Number</th>               
            <th>Item</th>                       
            <th>Quantity</th>                   
            <th>Status</th>                     
            <th>Order Id</th>                   
        </tr>   
        <tr>
            <td>{{ order.Table_No }}</td>       
            <td>{{ order.Item }}</td>           
            <td>{{ order.Qty }}</td>            
            <td>{{ order.food_status }}</td>    
            <td>{{ order.OrderId }}</td>        
        </tr>
    {% endfor %}
    </table>
<br><input action="action" onclick="window.history.go(-1); return false;" type="button" value="Back"></br>
</form>

The kitchen_page template should show the drop-down list, then the chef can choose the food_status from that drop-down list, click save button, and update the database.

Eve11
  • 99
  • 3
  • 14

2 Answers2

0

Try using choices attribute of Django fields https://docs.djangoproject.com/en/2.0/ref/models/fields/#choices

Umair Mohammad
  • 4,489
  • 2
  • 20
  • 34
0

You can render choices using {% for %} loop and FoodStatus list of choices like this:

<td>
    {{ order.get_food_status_display }}
    <select name="food_status">
        {% for id, choice in order.FoodStatus %}
            <option value="{{ id }}"{% if order.food_status == id %} selected="selected"{% endif %}>{{ choice }}</option>
        {% endfor %}
    </select>
</td>

You can display actual status text (instead of id), using get_FOO_display method.
Added {% if %} tag to preselect correct option.
Consider switching to Forms so it can handle rendering fields automatically.(!!!)
Consider switching food_status to IntegerField instead. Provide default attribute, so it will always be one of the choices, even if not specified.

Gasanov
  • 2,839
  • 1
  • 9
  • 21
  • Thank you for the reply. how to display "cooked", "has been ordered", etc in the template instead of showing 1, 2, 3, or 4? – Eve11 May 18 '19 at 09:55
  • i mean when retrieve the data on another page, it will show 1,2,3, or 4 instead of "cooked", "has been ordered", "ready to be served", and "done" – Eve11 May 18 '19 at 10:19
  • Which data you retrieve? Provide code how you doing it. – Gasanov May 18 '19 at 10:21
  • I have changed views.py and the template. Please refer to those codes – Eve11 May 18 '19 at 10:26
  • do you mean that you want to see the customer_order template? – Eve11 May 18 '19 at 10:32
  • Your view is providing context with only `so`, but in your template you are using `chef_view`. And that only for POST method. On top of that you don't have any of mine code. What did you try? – Gasanov May 18 '19 at 10:33
  • your code is located on different template and function – Eve11 May 18 '19 at 10:41
  • hence, do you know how to show the "cooked", etc instead of 1,2,3,4 on the see_order template? because it retrieves the data from OrderItem database too – Eve11 May 18 '19 at 10:47
  • It is correct for both cases. Read my answer carefully. – Gasanov May 18 '19 at 12:04