Django模型表单不显示bootstrap模式中的数据

时间:2015-01-07 12:56:40

标签: django twitter-bootstrap modal-dialog modelform

所以,我是django的新手,我想要实现的是在bootstrap模式中编辑注释。 我的问题是,当我启动模态以编辑特定注释时,注释文本不会以模态显示。我使用相同的CommentForm类来创建和编辑注释,因为我对我的应用程序中的其余模型类使用相同的原则。

以下是编辑评论视图:

def edit_comment(request, p_id):
   comment = get_object_or_404(Comment, pk=p_id)
   task_id = comment.task.id
   task = get_object_or_404(Task, pk=task_id)
   if request.method == "POST":
       form = CommentForm(request.POST, instance=comment)
       if form.is_valid():
           form.save()
           return redirect(reverse('task_details', kwargs={'p_id' : task.id}))
   else:
       data = {
            'form' : CommentForm(instance=comment),
              }
       return render(request, "polls/task_details.html",data)

因为附加到任务的所有注释的列表都显示在任务详细信息中,这是我启动编辑模式的地方,这里是视图:

def task_details(request, p_id):
    projects = Project.objects.filter(users=request.user)
    task = get_object_or_404(Task, pk=p_id)
    comments = Comment.objects.filter(task__id=p_id)
    proj_id = task.project.id
    project = get_object_or_404(Project, pk=proj_id)
    if request.method == "POST":    
        data = {'task': task,
            'comments': comments,
            'project' : project,
            'form' : CommentForm(),
            'projects' : projects}
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.user = request.user
            comment.type = "comment"
            comment.task = task
            comment.save()
            return render(request, "polls/task_details.html", data)
    else:        
        data = {'task': task,
               'comments': comments,
               'project' : project,
               'form' : CommentForm(),
               'projects' : projects}
        data.update(csrf(request))
    return render(request, "polls/task_details.html", data)

forms.py:

class CommentForm(ModelForm):

class Meta:
    model = Comment
    fields = ('comment_text',)
    exclude = ('user', 'time_occurred', 'type', 'task', 'milestone')
    widgets = {'comment_text': forms.Textarea(attrs={'class':"form-control",
                                       'required':"",
                                       'rows':3,                                            
                                       })}
    labels = {"comment_text": _("Comment"),
              }

urls.py:

url(r'^edit_comment/(?P<p_id>\d+)/$', views.edit_comment, {}, 'edit_comment'),

注释附加到Task,而Comment继承了LogEvent抽象类,所以这里是models.py部分:

class Task(models.Model):
    project = models.ForeignKey(Project)
    user = models.ForeignKey(User,  related_name='task_created_by')
    assigned = models.ForeignKey(User, related_name='task_assigned_to', null=True)
    priority = models.ForeignKey(Priority,null=True)
    milestone = models.ForeignKey(Milestone)
    name = models.CharField(max_length = 50)
    description = models.CharField(max_length = 5000)
    type = models.ForeignKey(Type, null=True)
    state = models.CharField(max_length = 20, choices = State)

    def __str__(self):
        return self.name

class LogEvent(models.Model):
    user = models.ForeignKey(User)
    time_occurred = models.DateTimeField('time_occurred')
    type = models.CharField(max_length = 50)
    task = models.ForeignKey(Task, null=True, blank=True)
    milestone = models.ForeignKey(Milestone, null=True, blank=True)

    class Meta:
        abstract = True

class Comment(LogEvent):
    comment_text = models.TextField()
    def save(self, *args, **kwargs):
        ''' On save, update timestamps '''
        if not self.id:
            self.time_occurred = datetime.datetime.today()
        return super(Comment, self).save(*args, **kwargs)

    def __str__(self):
        return self.user.username

最后,模板: (这是任务详细信息模板的一部分,其中显示了附加到特定任务的所有注释)

<div class="col-lg-12">
        {% for comment in comments %}
        <div class="well">
            <p>
                <i class="fa fa-fw fa-user"></i><a href="" style="color: #424242"><strong>{{comment.user.get_full_name}}</strong></a>
                <i class="fa fa-fw fa-clock-o"></i>{{comment.time_occurred}} 


                {% if comment.user == user %}
                 <a href=""
                    class="btn btn-info btn-xs btn-outline"
                    style="float: right; margin-left: 5px" data-toggle="modal" data-target="#EditModal{{forloop.counter}}"> <i class="fa fa-fw fa-edit"></i> Edit</a>


                <!-- EDIT MODAL --> 
                <div class="modal fade" id="EditModal{{forloop.counter}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                            <div class="modal-dialog" >
                                <div class="modal-content" style=" border-color: #5cb85c;">
                                    <div class="modal-header" style="  color: white;background-color: #5cb85c;border-color: #5cb85c;">
                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                        <h4 class="modal-title" id="myModalLabel" >Edit comment</h4>
                                    </div>
                                    <form action="{% url 'edit_comment' p_id=comment.id %}" method="post">
                                         {% csrf_token %}
                                         {% for field in form %}
                                         <div class="modal-body">
                                            {{field.errors}} 
                                            {{field}}
                                         </div>
                                        {% endfor %}
                                         <div class="modal-footer">
                                              <button type="button" class="btn btn-default btn-outline" data-dismiss="modal">Close</button>
                                              <input type="submit" class="btn btn-success btn-outline" value="Save changes">
                                         </div>
                                    </form>
                                </div>
                                <!-- /.modal-content -->
                            </div>
                            <!-- /.modal-dialog -->
                 </div>




            {% endif %}

            <p>{{comment.comment_text}}</p>
        </div>
        {% endfor %}
    </div>

我的问题是我应该怎么做才能在模态中的textarea中显示评论文本,或者我错过了什么,这样用户可以轻松地修改他的评论,而不是在发布模态之前复制文本?

0 个答案:

没有答案