我有以下基于类的视图,
Sub Make_Macro_Go_now()
Dim my_FileName As Variant
MsgBox "Pick your TOV file" '<--| txt box for prompt to pick a file
my_FileName = Application.GetOpenFilename(FileFilter:="Excel Files,*.xl*;*.xm*") '<--| Opens the file window to allow selection
If my_FileName <> False Then
Workbooks.Open FileName:=my_FileName
Call Filter_2 '<--|Calls the Filter Code and executes
End If
End Sub
Public Sub Filter_2()
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Dim rCountry As Range, helpCol As Range
Dim FileName As String
Dim s As Worksheet
Dim y As Workbook ''AT
Dim y_1 As Workbook ''BE
FileName = Right(ActiveWorkbook.Name, 22)
With ActiveWorkbook.Sheets(1) '<--| refer to data worksheet
With .UsedRange
Set helpCol = .Resize(1, 1).Offset(, .Columns.Count) '<--| get a "helper" column just at the right of used range, it'll be used to store unique country names in
End With
With .Range("A1:Q" & .Cells(.Rows.Count, 1).End(xlUp).Row) '<--| refer to its columns "A:Q" from row 1 to last non empty row of column "A"
.Columns(1).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=helpCol, Unique:=True '<-- call AdvancedFilter on 6th column of the referenced range and store its unique values in "helper" column
Set helpCol = Range(helpCol.Offset(1), helpCol.End(xlDown)) '<--| set range with unique names in (skip header row)
For Each rCountry In helpCol '<--| iterate over unique country names range (skip header row)
.AutoFilter 1, rCountry.Value2 '<--| filter data on country field (6th column) with current unique country name
If Application.WorksheetFunction.Subtotal(103, .Cells.Resize(, 1)) > 1 Then '<--| if any cell other than header ones has been filtered...
Worksheets.Add Worksheets(Worksheets.Count) '<--... add new sheet
ActiveSheet.Name = rCountry.Value2 & FileName '<--... rename it
.SpecialCells(xlCellTypeVisible).Copy ActiveSheet.Range("A1") 'copy data for country under header
End If
Next
End With
.AutoFilterMode = False '<--| remove autofilter and show all rows back
End With
helpCol.Offset(-1).End(xlDown).Clear '<--| clear helper column (header included)
''Copy and Paste Data
For Each s In Sheets
If s.Name = "DE_ITOV_MTNG_ATNDEE.xlsx" Then
s.Activate
ActiveSheet.Range("A1", ActiveCell.SpecialCells(xlLastCell)).Copy
Set y = Workbooks.Open("H:\TOV Storage Folder\Germany.xlsx")
y.Sheets(2).Name = "DE_ITOV_MTNG_ATNDEE"
y.Sheets("DE_ITOV_MTNG_ATNDEE").Range("A1").PasteSpecial Paste:=xlPasteFormulas
y.SaveAs "H:\TOV Storage Folder\Germany.xlsx"
y.Close
ElseIf s.Name = "BE_ITOV_MTNG_ATNDEE.xlsx" Then
s.Activate
ActiveSheet.Range("A1", ActiveCell.SpecialCells(xlLastCell)).Copy
Set y_1 = Workbooks.Open("H:\TOV Storage Folder\Belgium.xlsx")
y_1.Sheets(2).Name = "BE_ITOV_MTNG_ATNDEE"
y_1.Sheets("BE_ITOV_MTNG_ATNDEE").Range("A1").PasteSpecial Paste:=xlPasteFormulas
y_1.SaveAs "H:\TOV Storage Folder\Belgium.xlsx"
y_1.Close
''Exit Sub
End If
Next s
''MsgBox "Sheet a does not exist"
''End If
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Public Function DoesFileExist(ByVal sFile)
Dim oFSO As New FileSystemObject
If oFSO.FileExists(sFile) Then
DoesFileExist = True
Else
DoesFileExist = False
End If
End Function
将数据传递给以下表单。
class FacetUpdate(TenantRootedMixin, UpdateView):
model = Tenant
context_object_name = 'facetten'
form_class = FacetForm
def get_success_url(self):
kwargs = {'uuid': self.object.uuid}
return reverse_lazy('productupdate', kwargs=kwargs)
def get_success_url(self, **kwargs):
return reverse('facet_list', args=[self.request.tenant.slug])
def get(self, request, *args, **kwargs):
d = super(FacetUpdate, self).post(request, *args, **kwargs)
self.object = self.get_object()
return super(FacetUpdate, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
d = super(FacetUpdate, self).post(request, *args, **kwargs)
self.object = self.get_object()
#Create list of selected checkbox_ids
hitlist = request.POST.getlist('facet_link')
#Get current tenant
currenttenant = Tenant.objects.get(name=self.request.tenant)
#Get all facets that have been selected in the form
selected_facets = Facetss.objects.filter(id__in=hitlist)
#Update the currenttenant
currenttenant.linked_facets = selected_facets
currenttenant.save()
return d
此表单不使用自动生成的表单,而是使用包含复选框列表的自定义html页面。 (使用默认小部件意味着布局完全错误,因为我们需要嵌套布局)
表单有效,但缺少的一件事是根据正在编辑的实例预先填充复选框。 表单中的所有字段都显示为空,强制用户在编辑过程中重写所有数据。
当我使用基于函数的视图时,我必须在视图中定义一个“实例”,然后加载所有相关数据。然而,我正在为我目前正在研究的基于类的视图创建类似的东西。
有谁知道如何解决这个问题?
编辑:
我已将我用于下面表单的html页面包含在内
class FacetForm(forms.ModelForm):
class Meta:
model = Tenant
fields = ['linked_facets']
编辑2:
基于一些评论(谢谢!)我已经像这样编辑了视图的'GET'功能。
{% extends "layouts/tabbed_panel.html" %}
{% load i18n %}
{% load mptt_tags %}
{% block panel-header %}
<h1 class="text-primary">Facetten Aanpassen</h1>
{% endblock %}
{% block panel-body %}
<form action="." method="POST" class="FacetForm">{% csrf_token %}
<ul id="id_linked_facets">
{% recursetree facetten %}
<li>
<label><input type="checkbox" name= 'facet_link' value={{node.pk}}><strong>{{ node.name }}</strong></label>
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
</ul>
<br>
<div class="text-right">
<a href="{% url 'facet_list' tenant.slug %}" class="btn btn-default">{% block cancel-button-text %}{% trans "Cancel" %}{% endblock %}</a>
<input type="submit" class="btn btn-primary" value="{% trans 'Save' %}" />
</div>
</form>
{% endblock %}
当我将它打印到终端时,值似乎显示出来,但我仍然不清楚如何将其传递给实际模板。对不起,如果我遗漏了一些非常明显的东西,我仍然需要了解基于阶级的观点。
答案 0 :(得分:0)
感谢大家的回复! (尤其是dirkgroten) 我使用MPTT模块这一事实使一切变得更加复杂,但在您的帮助下,我能够通过进行2次更改来解决问题:
在视图中我添加了一个get_context_data:
def get_context_data(self, **kwargs):
d = super(FacetUpdate, self).get_context_data(**kwargs)
currenttenant = self.context.tenant
d['linked_facets'] = currenttenant.linked_facets.all()
print("CONTEXT", d)
return d
然后我使用了&#39; linked_facets&#39;模板中的值,以检查是否应该检查该框(从dirkgroten他的建议略有改变)
<label><input type="checkbox" name= 'facet_link' {% if node in linked_facets %}checked{%endif%} value={{node.pk}}><strong>{{ node.name }}</strong></label>
非常感谢大家,这个问题一直困扰着我好几个小时,而且最终解决了!