[Fixed]-Displaying a django variable as the value attribute of a form field

1👍

In your view, when initializing the form, you can assign initial values:

form = WhatsYourFormsClass(initial={'field1': value1, 'field2': value2})

EDIT:
You initialise the form with initial values correctly:

(...)
form = EditFeedbackForm(initial={'title': post.feedback_title, 'body': post.feedback_description})
if (...)

but then pass another (empty) instance:

(...)
else
    form = EditFeedbackForm()
return (...)

Your method should be like that:

@login_required
def edit_feedback(request, feedback_id, board):
    boardObj = Board.objects.get(board_name=board)
    post = Feedback.objects.get(id=feedback_id)
    if request.method == 'POST':
        form = EditFeedbackForm(request.POST)
        (form processing)
        (if processing went OK, redirect to your chosen view)
    else:
        form = EditFeedbackForm(initial={'title': post.feedback_title, 'body': post.feedback_description})
    return render(**your form template name here**, {'form': form})

Leave a comment