在django表单字段中显示数据库中的数据

时间:2016-07-05 18:04:55

标签: django django-forms

我的反馈论坛类型的网络应用程序有一个可用的编辑表单,但我无法弄清楚如何使帖子的内容出现在编辑表单中。我希望用户点击“编辑”并转到编辑反馈网址,并在表单中显示反馈帖的标题和正文。我正在使用Django,但我找不到任何关于如何实现这一目标的文档。

这是forms.py

中的EditFeedbackForm
class EditFeedbackForm(forms.Form):
    title = forms.CharField(label='Title')
    body = forms.CharField(label='Body', widget=forms.Textarea)
edit_feedback

中的

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})

edit_feedback.html模板

{% 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 %}

我目前的工作是在页面顶部显示原始帖子,以便用户可以复制并粘贴到表单中进行更改,但显然这并不理想。

1 个答案:

答案 0 :(得分:1)

您可以使用Django的Generic Update View

,而不是自己构建表单 来自文档的

片段:

  

一个视图,显示用于编辑现有对象的表单,重新显示包含验证错误的表单(如果有)并保存对象的更改。这使用从对象的模型类自动生成的表单(除非手动指定表单类)。

在上面链接的文档中甚至有足够的例子。