我正在尝试理解在 'Create an INSERT INTO SQL statement
Dim insertStatement As String = "INSERT INTO [DATA$] ([WEEK], [DATE], [PART NAME], [PART NUMBER], [REASON FOR SCRAP], [LINE], [MACHINE], [TOOLING], [MTO], [QTY], [WHO SCRAP IT]) VALUES ('" + WEEK_TEXT.Text + "','" + DATEE_TEXT.Text + "','" + PARTNAME_TEXT.Text + "','" + PARTNUMBER_TEXT.Text + "','" + SCRAP_REASON_TEXT.Text + "','" + LINE_text.Text + "','" + MACHINE_TEXT.Text + "','" + SIDE_TEXT.Text + "','" + MTO_TEXT.Text + "','" + QUANTITY_TEXT.Text + "','" + TL_TS_TEXT.Text + "')"
Dim insertStatement1 As String = "INSERT INTO [QAD DATA$] ([WEEK], [DATE], [PART NAME], [QAD NUMBER], [REASON FOR SCRAP], [LINE], [MACHINE], [TOOLING], [MTO], [QTY], [WHO SCRAP IT]) VALUES ('" + WEEK_TEXT.Text + "','" + DATEE_TEXT.Text + "','" + PictureBox1.I + "','" + QAD_NUMBER_TEXT.Text + "','" + SCRAP_REASON_TEXT.Text + "','" + LINE_text.Text + "','" + MACHINE_TEXT.Text + "','" + SIDE_TEXT.Text + "','" + MTO_TEXT.Text + "','" + QUANTITY_TEXT.Text + "','" + TL_TS_TEXT.Text + "')"
'Create a connection object to connect to the Excel Workbook
Dim connection As New OleDbConnection(connectionString)
Dim connection1 As New OleDbConnection(connectionString)
'Create a command object that will execute the insert statement
Dim command As New OleDbCommand(insertStatement, connection)
Dim command1 As New OleDbCommand(insertStatement1, connection1)
'Open the connection, execute the statement and close the connection
connection.Open()
connection1.Open()
command.ExecuteNonQuery()
command1.ExecuteNonQuery()
connection.Close()
connection1.Close()
'Dispose of the connection and command objects
connection.Dispose()
command.Dispose()
command1.Dispose()
中使用的一些源代码,以在页面底部生成分页链接。例如,在列出“专家”的页面上,它看起来像这样:
它似乎与https://docs.djangoproject.com/en/2.0/topics/pagination/中描述的方式略有不同。有一个名为ListView
的包含标记:
paginator
相应的模板from django import template
from django.core.paginator import EmptyPage
from dashboard.views.utils import query_dict_from_params_or_cookies
register = template.Library()
def query_params(params):
query = params.copy()
if query.get('page'):
del query['page']
return '&' + query.urlencode() if query.urlencode() else ''
@register.inclusion_tag('templatetags/paginator.html', takes_context=True)
def paginator(context, adjacent_pages=2):
request = context['request']
page_obj = context['page_obj']
page_range = context['paginator'].page_range
page_number = page_obj.number
last_page_number = len(page_range)
left_idx = max(0, page_number - adjacent_pages - 1)
right_idx = min(page_number + adjacent_pages, last_page_number)
if left_idx == 2:
left_idx -= 1
if right_idx == last_page_number - 2:
right_idx += 1
window = page_range[left_idx:right_idx]
try:
previous_page_number = page_obj.previous_page_number()
except EmptyPage:
previous_page_number = None
try:
next_page_number = page_obj.next_page_number()
except EmptyPage:
next_page_number = None
params = query_dict_from_params_or_cookies(request)
return {
'has_previous': page_obj.has_previous(),
'has_next': page_obj.has_next(),
'previous_page_number': previous_page_number,
'next_page_number': next_page_number,
'last_page_number': last_page_number,
'page_range': window,
'page_number': page_number,
'show_first': page_number > adjacent_pages + 1,
'show_left_gap': page_number > adjacent_pages + 3,
'show_last': page_number < last_page_number - adjacent_pages,
'show_right_gap': page_number < last_page_number - adjacent_pages - 2,
'query_params': query_params(params)
}
如下所示:
templatetags/paginator.html
我不太明白的是<ul class="pagination right"
{% if has_previous %}data-previous-page="?page={{ previous_page_number }}{{ query_params }}"{% endif %}
{% if has_next %}data-next-page="?page={{ next_page_number }}{{ query_params }}"{% endif %}>
{% if has_previous %}
<li class="waves-effect">
<a href="?page={{ previous_page_number }}{{ query_params }}">
<i class="material-icons">chevron_left</i>
</a>
</li>
{% else %}
<li class="disabled">
<a><i class="material-icons">chevron_left</i></a>
</li>
{% endif %}
{% if show_first %}
<li><a href="?page=1{{ query_params }}">1</a></li>
{% endif %}
{% if show_left_gap %}
<li class="disabled"><a>...</a></li>
{% endif %}
{% for i in page_range %}
{% if page_number == i %}
<li class="active"><a>{{ i }}</a></li>
{% else %}
<li class="waves-effect"><a href="?page={{ i }}{{ query_params }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if show_right_gap %}
<li class="disabled"><a>...</a></li>
{% endif %}
{% if show_last %}
<li>
<a href="?page={{ last_page_number }}{{ query_params }}">{{ last_page_number }}</a>
</li>
{% endif %}
{% if has_next %}
<li class="waves-effect">
<a href="?page={{ next_page_number }}{{ query_params }}">
<i class="material-icons">chevron_right</i>
</a>
</li>
{% else %}
<li class="disabled">
<a><i class="material-icons">chevron_right</i></a>
</li>
{% endif %}
</ul>
和paginator
如何插入到模板的上下文中。例如,我在项目范围内搜索了page_obj
,并且它似乎没有被我们自己的代码注入,这意味着它是由Django的源代码注入的。但这是怎么做到的?
另外,在我看来,有些逻辑基本上复制了Django的page_obj
类中的内容,这个代码可以简化/重构,有人会同意吗?
答案 0 :(得分:0)
看来这确实是由Django的源代码完成的 - 具体来说,是通用的ListView
。 get_context_data()
的{{1}}方法({1}}继承)读取:
MultipleObjectMixin
显然,此处将ListView
和def get_context_data(self, *, object_list=None, **kwargs):
"""Get the context for this view."""
queryset = object_list if object_list is not None else self.object_list
page_size = self.get_paginate_by(queryset)
context_object_name = self.get_context_object_name(queryset)
if page_size:
paginator, page, queryset, is_paginated = self.paginate_queryset(queryset, page_size)
context = {
'paginator': paginator,
'page_obj': page,
'is_paginated': is_paginated,
'object_list': queryset
}
else:
context = {
'paginator': None,
'page_obj': None,
'is_paginated': False,
'object_list': queryset
}
if context_object_name is not None:
context[context_object_name] = queryset
context.update(kwargs)
return super().get_context_data(**context)
添加到上下文中。