Reputation: 251
I've got a working edit form for my feedback forum type web app, but I can not figure out how to make the content of a post appear in the edit form. I want the user to click "edit" and get to the edit feedback url and have the title and body of the feedback post already in the form. I am using Django, but I can't find any documentation about how to accomplish this.
This is my EditFeedbackForm in forms.py
class EditFeedbackForm(forms.Form):
title = forms.CharField(label='Title')
body = forms.CharField(label='Body', widget=forms.Textarea)
edit_feedback
view in views.py
def edit_feedback(request, feedback_id, board):
boardObj = Board.objects.get(board_name=board)
boardGroups = boardObj.groups.all()
userGroups = request.user.groups.all()
userBoardAccess = False
boardFeedbacks = Feedback.objects.filter(feedback_board=boardObj.id)
userFeedbacks = Feedback.objects.filter(feedback_user=request.user.profile.id)
print boardFeedbacks
for group in boardGroups:
if group in userGroups:
userBoardAccess = True
break
if not userBoardAccess:
messages.error(request, 'Cannot view a board you are not a member of.')
return boards(request)
post = Feedback.objects.get(id=feedback_id)
form = EditFeedbackForm()
if post.feedback_user == request.user.profile:
if request.method == 'POST':
form = EditFeedbackForm(request.POST)
if form.is_valid():
title = form.cleaned_data['title']
body = form.cleaned_data['body']
post.feedback_title = title
post.feedback_description = body
post.feedback_last_modified = datetime.now()
post.save(update_fields=['feedback_title', 'feedback_description', 'feedback_last_modified'])
print "saved feedback " + str(post.id)
else:
form = EditFeedbackForm()
else:
messages.error(request, 'Cannot edit a post you did not write.')
return redirect('/board/' + board + '/')
return render(request, 'app/edit_feedback.html', {'form': form, 'board': boardObj.board_name, 'boardid': boardObj.id, 'boardObj': boardObj, 'feedback': post, 'userFeedbacks': userFeedbacks})
And the edit_feedback.html
template
{% extends 'app/base.html' %}
{% load i18n widget_tweaks %}
{% load board_extras %}
{% block content %}
<div id="body-wrapper">
<h1><a id="undecorated" href="/board/{{board}}">{{board}}</a></h1>
<!-- show current feedback -->
<div style="margin-left: 50px;">
<h4>{{feedback.feedback_title}} | {{feedback.feedback_last_modified }}</h4>
<h5>{{ feedback.feedback_description }}</h5>
</div>
<!-- edit form -->
<form role="form" action="" method="post">
{% csrf_token %}
{% for field in form %}
{% if field.errors %}
<div class="form-group has-error">
<label class="col-sm-2 control-label" for="id_{{ field.name }}">
{{ field.label }}</label>
<div class="col-sm-10">
{{ field|attr:"class:form-control" }}
<span class="help-block">
{% for error in field.errors %}
{{ error }}
{% endfor %}
</span>
</div>
</div>
{% else %}
<div class="form-group">
<label class="col-sm-2 control-label" for="id_{{ field.name }}">{{ field.label }}</label>
<div class="col-sm-10">
{{ field|attr:"class:form-control" }}
{% if field.help_text %}
<p class="help-block"><small>{{ field.help_text }}</small></p>
{% endif %}
</div>
</div>
{% endif %}
{% endfor %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary" value="Save">{% trans "Save" %}</button><br>
</div>
</div>
</form>
</div>
{% endblock %}
My current work around is to display the original post at the top of the page so a user could copy and paste into the form to make changes, but obviously that is not ideal.
Upvotes: 0
Views: 2361
Reputation: 1964
Instead of building the form yourself, you could use Django's Generic Update View.
Snippet from the docs:
A view that displays a form for editing an existing object, redisplaying the form with validation errors (if there are any) and saving changes to the object. This uses a form automatically generated from the object’s model class (unless a form class is manually specified).
There is even a sufficient example in the docs linked above.
Upvotes: 1