0

我在一个(下面的 html 页面)页面上使用了两种表单。一个表单 (create_task_form) 创建任务,另一个 (edit_task_form) 应该编辑/显示任务。为了让我自己更难,我决定将任务显示在一个列表中,当用户点击一个任务时,它应该在一个对话框中显示一个包含任务详细信息的表单。同样,创建对话框应该正常工作,但是我无法弄清楚如何使用相关任务信息从对话框中的列表中填充现有任务的(edit_task_form)编辑/显示表单。

Views.py:已编辑

    def status(request, project_id, task_id=None):
    need_owner_list = Task.objects.filter(project__id=project_id, status=0)
    in_progresss_list = Task.objects.filter(project__id=project_id, status=1)
    pending_review_list = Task.objects.filter(project__id=project_id, status=2)
    t = get_object_or_404(Task, pk=task_id) 
    p = get_object_or_404(Project, pk=project_id)


    if project_id:
        p = get_object_or_404(Project, pk=project_id)
    else:
        p = None
  #  if task_id:
  #      t = get_object_or_404(Task, pk=task_id)
  #  else:
  #      t = None


    if request.method == 'POST':
        p = get_object_or_404(Project, pk=project_id)
        post_task = PostTaskForm(request.POST)

        if post_task.is_valid():
            task = post_task.save(commit=False)
            task.project = p
            task.save()
            url = reverse('status', args=[project_id])
            return HttpResponseRedirect(url) 
    else:
        post_task = PostTaskForm()

    if request.method == 'POST':
        t = get_object_or_404(Task, pk=task_id)
        tform = PostTaskForm(request.POST, instance = t)

        if form.is_valid():
            task = tform.save()
            url = reverse('status', args=[project_id])
            return HttpResponseRedirect(url)    
    else:
        tform = PostTaskForm(instance=t)

    c = Context({
    'need_owner_list': need_owner_list,
    'in_progresss_list': in_progresss_list,
    'pending_review_list': pending_review_list,
    'project': p,
    'request': request,
    'pform': post_task,
    'task_id': task_id,
    'project_id': project_id,
    'task': t,
    'tform': tform,
    })
    return render_to_response('project/status.html', c, context_instance=RequestContext(request))

网页

{% extends "base.html" %}
{% block title %} project status {% endblock %}

{%block content %}
<body>

<head>

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/status.css"/>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/create_task.css"/>
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/status_box.css" />
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/list_items.css">
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/ownerless_task_list.css">

<!-- These are the scripts for the drag and drop functionality. -->
<script language="JavaScript" type="text/javascript" src="{{ STATIC_URL }}javascript/jquery.sortable.js"></script>    

</head>

<div id=lists_box>
    {% if need_owner_list %}
        <ul id="needs_owner" class="connected list">
            {% for task in need_owner_list|slice:":20" %}
                <li class="row1"><a href="#divModalDialog1" >{{ task.title }} {% url task task.id%}</a></li>
            {% endfor %}
        </ul>
    <div id="divModalDialog1" class="divModalDialog" type="hidden">
        <div id=task_div>
            <a href="">X</a>

       <form id="edit_task_form" action="{{ task }}" value="{{ task }}" method="POST">
                <input type="hidden" name="task" value="{{ task }}"/>

                    {% csrf_token %}
            {{ pform }}

                <input type="SubmitEdit" value="Submit Edit" onclick="self.close()">
            <input type="Reset" value="Reset">
           </form>
       </div>
    </div>
{% else %}
    <ul id="needs_owner" class="connected list">
        <li class="row1">No tasks are available.</li>
    </ul>    
{% endif %}


{% if in_progresss_list %}
    <ul id="in_progress" class="connected list">
        {% for task in in_progresss_list|slice:":20" %}
            <li class="row1"><a href="{% url edit_task task.pk %}">{{ task.title }}</a></li>
        {% endfor %}
    </ul>
{% else %}
    <ul id="in_progress" class="connected list">
        <li class="row1">No tasks are available.</li>
    </ul>
{% endif %}


{% if pending_review_list %}
    <ul id="pending_status" class="connected list">
        {% for task in pending_review_list|slice:":20" %}       
            <li class="row1"><a href="{% url task task.pk%}">{{ task.title }}</a></li>
        {% endfor %}
    </ul>
{% else %}
    <ul id="pending_status" class="connected list">
        <li class="row1">No tasks are available.</li>
    </ul>
{% endif %}
</div>

<!-- START:This section below is deals with the submit task popup window -->
{% if user.is_authenticated %}
    <script>
            $(function() {
                  $('.sortable').sortable();
                        $('.handles').sortable({
                            handle: 'span'
                            });
                        $('.connected').sortable({
                            connectWith: '.connected'
                            });
                        $('.exclude').sortable({
                            items: ':not(.disabled)'
                            });
            });
   </script>
<!-- The JS is added here to load after login. If this is added the the top of the page it conflicts with login_link.js -->
<script src="{{ STATIC_URL }}javascript/create_task.js" type="text/javascript"></script>
 <a id="submit_task">submit task</a>
    <div id="task_popout">
       <a id="task_popoutClose">X</a>

               {% if form.has_errors %}
                    <p>Your username and password didn't match. Please try again.</p>
               {% endif %}

       <form id="create_task_form" action="" value="submit_task" method="POST">
               <input type="hidden" name="project" value="{{ project_id }}"/>

                {% csrf_token %}
        {{ pform }}

               <input type="Submit" value="Submit Task" onclick="self.close()">
                <input type="Reset" value="Reset">
           </form> 

   </div>
   <div id="taskbackgroundPopup"></div>
{% else %}
    <p id="login_message">Please <a style="color:blue;" href="#authenticate"    id="task_chat">log in</a> to submit/edit tasks or particapate in chats.</p>
{% endif %}

    <div id="ticket_stats">
        <div id="owner_label" class="text">Owner:</div>
        <div id="owner" class="text">{{project.owner|safe}}</div>
        <div id="tags_label" class="text">Tags:</div>
        <div id="tags" class="text">{{project.tags|safe}}</div>
        <div id="created_label" class="text">Created:</div>
        <div id="created_date" class="text">{{project.date_created|date:"d/m/Y"}}</div>
        <div id="updated_label" class="text">Updated:</div>
        <div id="last_updated" class="text">{{project.date_updated|date:"d/m/Y"}}</div>
    </div>


</body>
{%endblock%}

模型.py

class Project(models.Model):
    title           = models.CharField(max_length=50, verbose_name='')
    slug            = models.SlugField(max_length=50, editable=False)
    owner           = models.ForeignKey(User, editable=False)
    problem         = tinymce_models.HTMLField(verbose_name='') 
    date_created    = models.DateTimeField(editable=False)
    date_updated    = models.DateTimeField(editable=False)
    tags            = TagField(verbose_name='')


    def set_tags(self, tags):
        Tag.objects.update_tags(self, tags)    

    def __unicode__(self):
        return self.tags

    def __unicode__(self):
        return self.id

    def __unicode__(self):
        return self.title

#This overrides the save function in the project/views.py module. It checks the
#created date against the updated date, and updates the updated date if needed.
#Also this takes the title and slugifies it so it can be rendered in the URL.
    def save(self, *args, **kwargs):
        if not self.id:
            self.date_created = datetime.now()
        self.date_updated = datetime.now()
        self.slug = slugify(self.title)
        super(Project, self).save(*args, **kwargs)


class PostProjectForm(ModelForm):
    class Meta:
        model = Project


STATUS_CHOICES = (
    ('0', 'Needs Owner'),
    ('1', 'In Progress'),
    ('2', 'Comp/Pend/Review'),
    ('3', 'Complete')
)

class Task(models.Model):
    title       = models.CharField(max_length=50)
    project        = models.ForeignKey(Project, editable=False)
    assignee    = models.ForeignKey(User, null=True, blank=True)
    task_desc   = models.TextField()
    solution    = models.TextField(blank=True)
    status = models.CharField(max_length=1, default='0', choices=STATUS_CHOICES)
    date_created    = models.DateTimeField(editable=False)
    date_updated    = models.DateTimeField(editable=False)

    def save(self, *args, **kwargs):
        if not self.id:
            self.date_created = datetime.now()
        self.date_updated = datetime.now()
        super(Task, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.id

    def __unicode__(self):
        return "%s" % self.object_pk

class PostTaskForm(ModelForm):
    class Meta:
        model = Task

URLs.py

url(r'^status/(?P<idea_id>\d+)$', 'status', name='status'),
url(r'^status/(?P<idea_id>\d+)/(?P<task_id>\d+)$', 'status', name='status_task'),

TL;博士

如何获取 edit_task_form 以显示来自任务的相关数据,以便用户可以编辑/查看任务?

提前致谢!

4

1 回答 1

0

不确定,但是,你有...

if request.method == 'POST':
    p = get_object_or_404(Project, pk=project_id)
    post_task = PostTaskForm(request.POST)
    ...
else:
    post_task = PostTaskForm()

^ 所以从上面看,如果您使用 javascript GET 请求 (?)

于 2012-08-03T20:28:52.757 回答