我有一个简单的应用程序,它接受条目/任务并将它们分配给适当的收件箱。 模型,模板和视图如下。我想要做的是修改视图,以便我可以 在我的表单中有一个功能“删除”按钮(与“添加/提交”按钮一起,就像在管理表单中一样。):
models.py
class InboxEntry(models.Model):
job_number = models.CharField(validators=[RegexValidator(regex='^\w{8}$', message='Please enter a valid job number', code='nomatch')], max_length=8, unique=False, blank=False, null=False)
cell_number = models.CharField(max_length=4, unique=False, blank=True, null=True)
job_name = models.CharField(max_length=64, unique=False, blank=False, null=False)
request = models.CharField(max_length=64, choices=PRINT_CHOICES)
date_in = models.DateField(("Date"), auto_now=True, auto_now_add=True)
date_due = models.DateTimeField(("Due"),auto_now=False, auto_now_add=False)
basecamp_link = models.URLField(validators=[RegexValidator(regex='^(http|https)://', message='url must begin with http or https', code='nomatch')], blank=True, null=False, max_length=300)
note = models.TextField(max_length=1000, unique=False, blank=True, null=True)
assigned_by = models.ForeignKey(UserProfile, blank=False, null=False)
box = models.CharField(max_length=64, choices=INBOX_CHOICES)
assigned_to = models.ForeignKey(UserProfile, related_name='+', blank=True, null=True)
assigned_team = models.ManyToManyField(ProjectTeam, blank=True, null=True)
status = models.CharField(max_length=30, choices=STATUS_CHOICES, default="Awaiting Action")
accepted_by = models.ForeignKey(UserProfile, related_name='+', blank=True, null=True)
completed_on = models.DateTimeField(("Completed"),auto_now=False, auto_now_add=False, blank=True, null=True)
def __unicode__ (self):
return u'%s %s' % (self.job_number, self.job_name)
class Admin:
pass
views.py
def edit_prdInboxEntry(request, id, userid):
if request.method == 'POST':
a=InboxEntry.objects.get(pk=id)
form = PrdInboxForm(request.POST, instance=a)
if form.is_valid():
form.save()
return HttpResponseRedirect('/production-display/'+ userid +'/')
else:
a=InboxEntry.objects.get(pk=id)
form = PrdInboxForm(instance=a)
user = request.user
u = UserProfile.objects.get(pk=userid)
creativerecords = InboxEntry.objects.filter(box="Creative")
studiorecords = InboxEntry.objects.filter(box="Studio")
records = InboxEntry.objects.filter(assigned_to=u)
return render_to_response('production_InboxEntry.html', {'form': form, 'records': records, 'studiorecords': studiorecords, 'creativerecords': creativerecords, 'user': user}, context_instance=RequestContext(request))
template
<form action="." method="POST">{% csrf_token %}
<div style="display:table;">
<div style="display:table-row;">
<div style="display:table-cell;">
<div class="group_input">
{{ form.assigned_by.errors }}
<label for="id_jassigned_by">Assigned by:</label>
{{ form.assigned_by }}
</div>
</div>
<div style="display:table-cell;"> <div>
<div style="display:table-cell;"> <div>
<div style="display:table-cell;"> <div>
<div style="display:table-cell;"> <div>
<div style="display:table-cell;"> </div>
</div>
<div style="display:table-row;">
<div style="display:table-cell;">
<div class="group_input">
{{ form.job_number.errors }}
<label for="id_job_number">job no:</label>
{{ form.job_number }}
</div>
</div>
<div style="display:table-cell;">
<div class="group_input">
{{ form.cell_number.errors }}
<label for="id_cell_number">cell:</label>
{{ form.cell_number }}
</div>
</div>
<div style="display:table-cell;">
<div class="group_input">
{{ form.job_name.errors }}
<label for="job_name">job name:</label>
{{ form.job_name }}
</div>
</div>
<div style="display:table-cell;">
<div class="group_input">
{{ form.request.errors }}
<label for="request">request:</label>
{{ form.request }}
</div>
</div>
<div style="display:table-cell;">
<div class="group_input">
{{ form.note.errors }}
<label for="id_note">note:</label>
{{ form.note }}
</div>
</div>
<div style="display:table-cell;">
<div class="container">
<!-- <div class='well'> -->
<div class="form-group">
<div class='input-group date' id='datetimepicker1'>
<label for="id_date_due">date_due:</label>
{{ form.date_due}}
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<!-- </div> -->
<script type="text/javascript">
$(function () {
$('#datetimepicker1').datetimepicker();
});
</script>
</div>
</div>
</div> <!-- end css table-row -->
<div style="display:table-row;">
<div style="display:table-cell;">
<div class="group_input">
{{ form.box.errors }}
<label for="id_box">inbox:</label>
{{ form.box }}
</div>
</div>
<div style="display:table-cell;">
<div class="group_input">
{{ form.assigned_to.errors }}
<label for="assigned_to">assigned_to:</label>
{{ form.assigned_to }}
</div>
</div>
<div style="display:table-cell;">
<div class="group_input">
{{ form.basecamp_link.errors }}
<label for="id_basecamp_link">basecamp:</label>
{{ form.basecamp_link }}
</div>
</div>
<div style="display:table-cell;">
<div class="accepted_by">
{{ form.accepted_by.errors }}
<label for="accepted_by">accepted_by:</label>
{{ form.accepted_by }}
</div>
</div>
<div style="display:table-cell;">
<div class="group_input">
{{ form.status.errors }}
<label for="id_status">status:</label>
{{ form.status }}
</div>
</div>
<div style="display:table-cell;">
<div class="container">
<!-- <div class='well'> -->
<div class="form-group">
<div class='input-group date' id='datetimepicker2'>
<label for="id_completed_on">completed:</label>
{{ form.completed_on}}
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<!-- </div> -->
<script type="text/javascript">
$(function () {
$('#datetimepicker2').datetimepicker();
});
</script>
</div>
</div>
</div> <!-- end css table-row -->
<div style="display:table-row;">
<div style="display:table-cell;"> </div>
<div style="display:table-cell;">
<p><input id="inbox_submit_btn" type="submit" value="add" /></p>
</div>
<div style="display:table-cell;">
<p><input id="inbox_delete_btn" type="submit" value="delete" /></p>
</div>
</div>
</div> <!-- end css table -->
</form>
任何人都可以帮助我指出正确的方向。文档似乎暗示我需要单独的视图和网址,但我很确定这可以在一个视图中完成?
答案 0 :(得分:0)
The docs seem to suggest that I need separate views and urls but I'm pretty sure this can be accomplished in a single view?
我猜你可以通过传入不同的POST参数来实现这一点,具体取决于按下了哪个按钮。尽管如此,这仍然是一件非常奇怪的事情。将它们分成不同的视图更加整洁,更易于维护。
对于链接到模板的视图,您可能会感到误解。有两个单独的视图(一个用于提交,一个用于删除)绝不会影响您显示链接到模板中这些视图网址的两个按钮的能力。
如果你担心代码重用(基于函数的视图经常共享大量代码,那么这是一个合理的抱怨)那么我建议调查class based views
编辑:
只是使用按钮链接到第二个“删除”视图:
<div class="your-button-class" href="{% url "your-delete-view" pk=object.pk %}></div>