0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I've included my edit feedback view, is there a way for me to incorporate UpdateView into what I have or would I need to start over? I found the example they provide to be overly simple
Try rewriting the view by subclassing UpdateView. Disregard what you've already written and follow the example in the docs. Once you get that working you can try writing your own custom view. But for what you're trying to do the overly simple example is all you need.
@Catherine I've just started using the generic views myself after having rolled my own views in the past. They're really useful. I found I got a better understanding when I read their Django source, though -- it is mixin heavy, but the code is fairly clear. They're nice to use because you can change your code to be mostly declarative rather than procedural ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.