如何在Django中根据ID过滤chartOfAccounts列表数据并在HTML模板中展示单个对象以实现编辑功能
Hey there! To solve this, you’ll need to fetch the specific account record in your Django view using the ID passed from the edit button, then populate your form template with the corresponding data. Here’s how to do it step by step:
1. Fetch the Account in Your View
First, create a view that retrieves the account using the ID from the URL. We use get_object_or_404 to handle cases where the ID doesn’t exist:
# views.py from django.shortcuts import get_object_or_404, render from .models import ChartOfAccount # Replace with your actual model name def edit_account(request, account_id): # Fetch the account by ID account = get_object_or_404(ChartOfAccount, id=account_id) # Pass the account object to the template context = {'account': account} # If you need to populate account detail type options, add them here # context['account_detail_types'] = AccountDetailType.objects.all() return render(request, 'edit_account.html', context)
2. Set Up the URL Route
Add a URL pattern that captures the account ID and maps to your edit view:
# urls.py from django.urls import path from . import views urlpatterns = [ # Other URLs... path('accounts/edit/<int:account_id>/', views.edit_account, name='edit_account'), ]
3. Populate the HTML Form Template
Update your form template to use the account object’s attributes. I’ve included the required fields plus logic to respect the is_editable flag from your data:
<!-- edit_account.html --> <form method="post"> {% csrf_token %} <div class="form-group row"> <label class="col-form-label col-md-2">Name</label> <div class="col-md-10"> <input type="text" class="form-control" name="name" value="{{ account.name }}" {% if not account.is_editable %}readonly{% endif %}> </div> </div> <div class="form-group row"> <label class="col-form-label col-md-2">Description</label> <div class="col-md-10"> <input type="text" class="form-control" name="desc" value="{{ account.description }}" {% if not account.is_editable %}readonly{% endif %}> </div> </div> <div class="form-group row"> <label class="col-form-label col-md-2">Balance</label> <div class="col-md-10"> <input type="text" class="form-control" name="bal" value="{{ account.balance }}" {% if not account.is_editable %}readonly{% endif %}> </div> </div> <div class="form-group row"> <label class="col-form-label col-md-2">Account Detail Type</label> <div class="col-md-10"> {% if account.is_editable %} <!-- Editable: Use a dropdown with all available types --> <select class="form-control" name="account_detail_type"> {% for type in account_detail_types %} <option value="{{ type.id }}" {% if type.id == account.account_detail_type.id %}selected{% endif %}> {{ type.name }} </option> {% endfor %} </select> {% else %} <!-- Non-editable: Show read-only text --> <input type="text" class="form-control" value="{{ account.account_detail_type.name }}" readonly> <input type="hidden" name="account_detail_type" value="{{ account.account_detail_type.id }}"> {% endif %} </div> </div> <div class="form-group row"> <div class="col-md-10 offset-md-2"> <button type="submit" class="btn btn-primary">Save Changes</button> </div> </div> </form>
Example for ID 147
If the user edits account ID 147, the form will automatically populate with:
- Name:
Sales Product - Wholesale test - Description:
Sales - Wholesale - Balance:
0.00 - Account Detail Type:
Accounts Receivable (A/R)
This setup ensures the correct data loads based on the clicked record’s ID and honors the editable status of each field.
内容的提问来源于stack exchange,提问作者MD. RONY AHMED

