我实现了Django(1.8)Web项目并将MongoDB用于我的数据库。我有视图,它显示表数据。我想为那张桌子加分。
这是我的 view.py 类
@login_required
def key_view(request):
client = MongoClient()
db = client.product.serial_key
cursor = db.find()
context = {'key': cursor}
return render(request, 'marcador/key_list_form.html',context)
我的 Model.py 类
@python_2_unicode_compatible
class Key_Gen(models.Model):
key_beging_code =models.DecimalField('Brand_name +Pack_size' ,max_digits=5, decimal_places=2)
key_quantity =models.DecimalField("key Quantity" ,max_digits=19, decimal_places=10)
key_code =models.CharField('key Code' ,max_length=50 , unique=True)
is_public = models.BooleanField('public', default=True)
date_created = models.DateTimeField('date created',auto_now_add=True)
date_updated = models.DateTimeField('date updated',auto_now_add=True)
class Meta:
verbose_name='key_Gen'
verbose_name_plural='keys_Gen'
ordering = ['-date_created']
def __str__(self):
return '%s' % (self.key_code)
def get_key_code(self,x,y):
alist = []
x = algo(int(x),int(y),alist)
keys =x.id_generator()
return keys
URL.py 文件
url(r'^view_key/$', 'marcador.views.key_view',
name='marcador_key_view'),
模板文件
{% block content %}
<table border="1">
<th width="100">Product Code</th>
<th width="100">Serial Key</th>
{% for x in key %}
<tr>
<td>{{ x.b_p_code }}</td>
<td>{{ x.key }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}
我是Dijango网络编程的新手,请期待一些专家的帮助。
我页面的当前视图。
根据第一个答案更新代码。
View.py
@login_required
def key_view(request):
client = MongoClient()
db = client.product.serial_key
cursor = db.find()
paginator = Paginator(cursor, 25) # Show 25 contacts per page
page = request.GET.get('page')
try:
cursor = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
cursor = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
cursor = paginator.page(paginator.num_pages)
return render_to_response('list.html', {'key': cursor})
现在给出以下错误。
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/view_key/
Django Version: 1.8
Python Version: 3.4.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'marcador',
'crispy_forms',
'bootstrap_pagination')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Template error:
In template /home/umayanga/Desktop/view_site/serialKey_gen_site/marcador/templates/marcador/key_list_form.html, error at line 13
object of type 'Cursor' has no len()
3 : {% block title %}Key List{% endblock %}
4 :
5 : {% block heading %}
6 : <h2>Keys</h2>
7 : {% endblock %}
8 :
9 : {% block content %}
10 : <table border="1">
11 : <th width="100">Product Code</th>
12 : <th width="100">Serial Key</th>
13 : {% for x in key %}
14 : <tr>
15 : <td>{{ x.b_p_code }}</td>
16 : <td>{{ x.key }}</td>
17 : </tr>
18 :
19 : {% endfor %}
20 : </table>
21 :
22 : {% endblock %}
23 :
Traceback:
File "/home/umayanga/Desktop/serialKey_gen_site/myvenv/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/umayanga/Desktop/serialKey_gen_site/myvenv/lib/python3.4/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "/home/umayanga/Desktop/view_site/serialKey_gen_site/marcador/views.py" in key_view
104. return render_to_response('marcador/key_list_form.html', {'key': cursor})
File "/home/umayanga/Desktop/serialKey_gen_site/myvenv/lib/python3.4/site-packages/django/shortcuts.py" in render_to_response
39. content = loader.render_to_string(template_name, context, using=using)
File "/home/umayanga/Desktop/serialKey_gen_site/myvenv/lib/python3.4/site-packages/django/template/loader.py" in render_to_string
99. return template.render(context, request)
File "/home/umayanga/Desktop/serialKey_gen_site/myvenv/lib/python3.4/site-packages/django/template/backends/django.py" in render
74. return self.template.render(context)
File "/home/umayanga/Desktop/serialKey_gen_site/myvenv/lib/python3.4/site-packages/django/template/base.py" in render
209. return self._render(context)
File "/home/umayanga/Desktop/serialKey_gen_site/myvenv/lib/python3.4/site-packages/django/template/base.py" in _render
201. return self.nodelist.render(context)
File "/home/umayanga/Desktop/serialKey_gen_site/myvenv/lib/python3.4/site-packages/django/template/base.py" in render
903. bit = self.render_node(node, context)
File "/home/umayanga/Desktop/serialKey_gen_site/myvenv/lib/python3.4/site-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/home/umayanga/Desktop/serialKey_gen_site/myvenv/lib/python3.4/site-packages/django/template/loader_tags.py" in render
135. return compiled_parent._render(context)
File "/home/umayanga/Desktop/serialKey_gen_site/myvenv/lib/python3.4/site-packages/django/template/base.py" in _render
201. return self.nodelist.render(context)
File "/home/umayanga/Desktop/serialKey_gen_site/myvenv/lib/python3.4/site-packages/django/template/base.py" in render
903. bit = self.render_node(node, context)
File "/home/umayanga/Desktop/serialKey_gen_site/myvenv/lib/python3.4/site-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/home/umayanga/Desktop/serialKey_gen_site/myvenv/lib/python3.4/site-packages/django/template/loader_tags.py" in render
65. result = block.nodelist.render(context)
File "/home/umayanga/Desktop/serialKey_gen_site/myvenv/lib/python3.4/site-packages/django/template/base.py" in render
903. bit = self.render_node(node, context)
File "/home/umayanga/Desktop/serialKey_gen_site/myvenv/lib/python3.4/site-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/home/umayanga/Desktop/serialKey_gen_site/myvenv/lib/python3.4/site-packages/django/template/defaulttags.py" in render
162. len_values = len(values)
File "/home/umayanga/Desktop/serialKey_gen_site/myvenv/lib/python3.4/site-packages/django/core/paginator.py" in __len__
117. return len(self.object_list)
Exception Type: TypeError at /view_key/
Exception Value: object of type 'Cursor' has no len()
答案 0 :(得分:1)
使用:
Paginator
自:
django.core.paginator
以下是文档:https://docs.djangoproject.com/en/1.8/topics/pagination/