Hi everyone, so I have been trying to code this and I have been seeing everyone’s code, I am pretty new to Django and not really sure how it works but what I am trying to do is display the choicefield value in template.
This is my models.py
class Orders(models.Model):
order_choices = (
('王者', 'wangzhe'),
('小乔', 'xiaoqiao'),
('打野', 'da ye'),
('伽罗', 'jia luo'),
)
progress = (
("In Progress","In Progress"),
("Completed","Completed")
)
account = models.ForeignKey('users.AccountUser', on_delete=models.CASCADE)
member = models.ManyToManyField('users.Member')
order_type = models.CharField(max_length=255, choices=order_choices)
status = models.CharField(max_length=50, choices=progress, default="In Progress")
order_number = models.CharField(max_length=12, unique=True)
def get_order_choices(self):
return self.order_choices
def get_progress(self):
return self.progress
def __str__(self):
return self.order_type
While this is my views.py
from users.models import Member
from .forms import OrderForm
from .filters import OrderFilter
from .models import Orders
import os
@login_required
def services(request):
if request.method == "POST":
form = OrderForm(request=request, data=request.POST)
if form.is_valid():
order = form.save()
messages.success(request, "Order Submitted!")
else:
for key, error in list(form.errors.items()):
messages.error(request, error)
else:
form = OrderForm(request)
choices = []
for c in Orders.order_type.field.choices:
choices.append(c[0])
return render(
request=request,
template_name = "services/services.html",
context={"form": form,
"choices": choices,
"member_id": request.user.member_id
}
)
@login_required
def orderSearch(request):
orders = Orders.objects.all()
myFilter = OrderFilter(request.GET, queryset=orders)
orders = myFilter.qs
return render(
request=request,
template_name = "services/orderSearch.html",
context={
"orders": orders,
"myFilter": myFilter
}
)
and this is my template
<div class="container">
<div class="row">
<div class="content-section">
{% for choice in choices %}
<div class="col-md-4">
<div class="card">
<img class="card-img-top" src="{% static 'images/1.png' %}">
<div class="card-body">
<h5 class="card-title">{{choice}}</h5>
<p class="card-text">Description testing</p>
<!--<button id="openModal">Order Now</button>-->
<button class="openModal">Order Now</button>
</div>
</div>
</div>
{% endfor %}
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close" id="closeModal">×</span>
<!-- Display the static information you want to show -->
<p> {{{ choice.get_order_choices_display }} </p>
<!-- Include your form here without an action attribute -->
<form method="post" id="modalForm" action="{% url 'services' %}">
{% csrf_token %}
<!-- Add your form fields here -->
<input type="hidden" name="member_id" id="id_member_id" value="{{ member_id }}">
<input type="hidden" name="order_type" id="id_order_type" value="补运">
{{ form.member_name }}
<button type="submit" id="submitForm">Submit</button>
</form>
</div>
</div>
</div>
</div>
</div>
I am unable to get the display for the {{{ choice.get_order_choices_display }} inside the modal. My card title has no issue in displaying the names of the choicefield.
Do let me know if require more information