如何将Django视图数据传递至HTML模板?附代码场景咨询
Hey there! Let's walk through creating the HTML template for your VerifiedBySuperuser view. First, your view handles POST requests to save a question entry, so we need a form that sends the required fields correctly. Here's a complete, working template:
<!-- exam48app/verifiedbysuperuser.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Verify Question (Superuser)</title> <style> /* Optional basic styling to make it look clean */ .form-group { margin: 1rem 0; } label { display: block; margin-bottom: 0.5rem; } input, textarea { width: 100%; padding: 0.5rem; box-sizing: border-box; } button { padding: 0.5rem 1rem; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; } </style> </head> <body> <h1>Verify & Save Question</h1> <!-- Success message (optional, if you add it to the view) --> {% if success_message %} <p style="color: green; font-weight: bold;">{{ success_message }}</p> {% endif %} <form method="POST"> <!-- Mandatory CSRF token for Django POST forms --> {% csrf_token %} <!-- Question ID Field --> <div class="form-group"> <label for="question-id">Question ID:</label> <input type="text" id="question-id" name="QuestionID" required placeholder="Enter question ID"> </div> <!-- English Question Text Field --> <div class="form-group"> <label for="question-english">Question (English):</label> <textarea id="question-english" name="QuestionInEnglishLang" rows="5" required placeholder="Type the question in English"></textarea> </div> <button type="submit">Save Verified Question</button> </form> </body> </html>
Key Things to Note:
- CSRF Token: The
{% csrf_token %}tag is non-negotiable for Django POST forms—it prevents cross-site request forgery attacks. Don't skip this! - Field Names: The
nameattributes of the input/textarea elements must match exactly what your view is looking for (QuestionIDandQuestionInEnglishLang). That's howrequest.POST.get()pulls the values from the form. - Required Attributes: Adding
requiredgives basic client-side validation, but you should always add server-side checks too (like making sure the QuestionID is valid, or the text isn't empty) to handle cases where users bypass client-side rules.
Quick Fix for Your View Code:
I spotted a potential bug in your view that you'll want to fix:
# Current problematic line vbs.QuestionID = MaanyaIT_EXAM48_ManageQuestionBank.objects.get(QuestionID=request.POST.get(QuestionID, None))
- The
request.POST.get(QuestionID, None)part usesQuestionIDas a variable, but it should be a string literal:request.POST.get('QuestionID', None)(notice the quotes around'QuestionID'). - If
QuestionIDis a regular field (like an integer or string ID) on your model, assigning an entireMaanyaIT_EXAM48_ManageQuestionBankinstance to it is wrong. You probably want to just assign the value from the form:
Ifvbs.QuestionID = request.POST.get('QuestionID', None)QuestionIDis a foreign key to another model, you'd need to fetch the related object instead (but that would require a different field name, likeQuestioninstead ofQuestionID—double-check your model definition!).
Optional: Adding Success Feedback
To make the user experience better, you can modify your view to send a success message after saving:
def VerifiedBySuperuser(request): if request.method == 'POST': vbs = MaanyaIT_EXAM48_ManageQuestionBank() vbs.QuestionID = request.POST.get('QuestionID', None) # Fixed line vbs.QuestionInEnglishLang = request.POST.get('QuestionInEnglishLang', None) vbs.save() # Pass success message to the template return render(request, 'exam48app/verifiedbysuperuser.html', {'success_message': 'Question saved successfully!'}) else: return render(request, 'exam48app/verifiedbysuperuser.html')
The template above already includes code to display this message if it exists.
That should get you up and running! Let me know if you need help with anything else.
内容的提问来源于stack exchange,提问作者pawas kr. singh
相关产品推荐
相关产品推荐

