You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将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 name attributes of the input/textarea elements must match exactly what your view is looking for (QuestionID and QuestionInEnglishLang). That's how request.POST.get() pulls the values from the form.
  • Required Attributes: Adding required gives 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))
  1. The request.POST.get(QuestionID, None) part uses QuestionID as a variable, but it should be a string literal: request.POST.get('QuestionID', None) (notice the quotes around 'QuestionID').
  2. If QuestionID is a regular field (like an integer or string ID) on your model, assigning an entire MaanyaIT_EXAM48_ManageQuestionBank instance to it is wrong. You probably want to just assign the value from the form:
    vbs.QuestionID = request.POST.get('QuestionID', None)
    
    If QuestionID is a foreign key to another model, you'd need to fetch the related object instead (but that would require a different field name, like Question instead of QuestionID—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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:33:15