【发布时间】:2015-08-11 01:26:21
【问题描述】:
当我单击 HTMl 中标签中的一行时,我试图在 Ddjango 中创建一些视图,我需要一些建议,请考虑到我是 Django 的初学者。
事实上,当我点击它时,我想改变我的视图,什么会打开一个新视图,就像我的标签内的Hash。
让我用这个名为bap2pmonitoring.html的代码来解释你:
{% load staticfiles %}
<!DOCTYPE html>
<html lang="fr">
<head>
<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}" />
</head>
<body>
<h1> BAP2P Monitoring</h1>
<table>
<tr>
<th width=550 height=20>Torrent Hash</th>
<th width=720 height=20>Torrent Name</th>
<th width=120 height=20>Size</th>
<th width=170 height=20>Active Peers</th>
</tr>
{% for torrent in torrents %}
<p id="demo">
<tr bgcolor=eeeeee>
<td width=550 height=20><a href="{{ url 'hash' torrent.Hash }}">{{ torrent.Hash }}</a></td>
<td width=720 height=20>{{ torrent.Name }}</td>
<td width=120 height=20>{{ torrent.Size }}</td>
<td width=170 height=20></td>
</tr>
</p>
{% endfor %}
</table>
</body>
</html>
我因此得到这个结果:
![在此处输入图片描述][1]
我的想法是,当我点击这两行之一时,它会呈现一个新视图,其中包含有关此种子的信息,其中种子的哈希为 url,如下所示:
127.0.0.1:8000/torrents/606d4759c464c8fd0d4a5d8fc7a223ed70d31d7b
按照 Django 教程,我尝试了很多事情都没有成功,然后我尝试了一个“onclick”在我的 view.py 中启动我的 def 之一,如下所示:
from django.shortcuts import render_to_response
from django.template import Template , Context
from polls.models import Torrent
# Create your views here.
# -*- coding: utf-8 -*-
def home(request):
return render_to_response('mysite/bap2pmonitoring.html', {'torrents':Torrent.objects.all()})
def details(request, torrent_hash):
return render_to_response('mysite/detail_torrent.html', {'torrents':Torrent.objects.filter(hash=torrent_hash)})
我还尝试在urls.py 中将哈希显示为这样的网址:
from django.conf.urls import patterns,include, url
from django.contrib import admin
from polls.models import Torrent
urlpatterns = patterns('polls.views',
url(r'^torrents$', 'home', name = 'home'),
url(r'^torrents/(?P<torrent_hash>)/$', 'details', name = 'hash'),
url(r'^admin/', include(admin.site.urls)),
)
我不明白我该如何解决这个问题,欢迎和赞赏任何想法
现在我得到这个错误页面:
NoReverseMatch at /torrents
Reverse for 'hash' with arguments '(u'606d4759c464c8fd0d4a5d8fc7a223ed70d31d7b',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['torrents/(?P<torrent_hash>)/$']
还有这个 Traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/torrents
Django Version: 1.8.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls')
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/florian/Documents/mysite/templates/mysite/bap2pmonitoring.html, error at line 23
Reverse for 'hash' with arguments '(u'606d4759c464c8fd0d4a5d8fc7a223ed70d31d7b',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['torrents/(?P<torrent_hash>)/$']
13 : <tr>
14 : <th width=550 height=20>Torrent Hash</th>
15 : <th width=720 height=20>Torrent Name</th>
16 : <th width=120 height=20>Size</th>
17 : <th width=170 height=20>Active Peers</th>
18 : </tr>
19 :
20 : {% for torrent in torrents %}
21 : <p id="demo">
22 : <tr bgcolor=eeeeee>
23 : <td width=550 height=20><a href=" {% url 'hash' torrent.Hash %} ">{{ torrent.Hash }}</a></td>
24 : <td width=720 height=20>{{ torrent.Name }}</td>
25 : <td width=120 height=20>{{ torrent.Size }}</td>
26 : <td width=170 height=20></td>
27 : </tr>
28 : </p>
29 : {% endfor %}
30 :
31 : </table>
32 :
33 : </body>
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/florian/Documents/mysite/polls/views.py" in home
8. return render_to_response('mysite/bap2pmonitoring.html', {'torrents':Torrent.objects.all()})
File "/usr/local/lib/python2.7/dist-packages/django/shortcuts.py" in render_to_response
39. content = loader.render_to_string(template_name, context, using=using)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py" in render_to_string
99. return template.render(context, request)
File "/usr/local/lib/python2.7/dist-packages/django/template/backends/django.py" in render
74. return self.template.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
209. return self._render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in _render
201. return self.nodelist.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
903. bit = self.render_node(node, context)
File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py" in render_node
79. return node.render(context)
File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py" in render
217. nodelist.append(node.render(context))
File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py" in render
507. six.reraise(*exc_info)
File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py" in render
493. url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in reverse
579. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in _reverse_with_prefix
496. (lookup_view_s, args, kwargs, len(patterns), patterns))
Exception Type: NoReverseMatch at /torrents
Exception Value: Reverse for 'hash' with arguments '(u'606d4759c464c8fd0d4a5d8fc7a223ed70d31d7b',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['torrents/(?P<torrent_hash>)/$']
【问题讨论】:
-
我相当肯定写一些 Javascript/jQuery 是你的未来。
-
目前您正在将 Torrent.objects.all() 作为上下文传递到您的详细信息视图中。您需要访问单个 torrent,而不是全部,例如 Torrent.objects.filter(torrent.Hash=Hash),具体取决于您作为 PK 的内容
标签: python html django python-2.7 django-views