当我在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]
我的想法是,当我单击这两行之一时,它将使用该torrent的哈希作为url来渲染有关该torrent信息的新视图,如下所示:
127.0.0.1:8000/torrents/606d4759c464c8fd0d4a5d8fc7a223ed70d31d7b
遵循Django教程之后,我尝试了很多事情而没有成功,然后尝试使用“ onclick”来启动我的def之一,view.py
如下所示:
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>)/$']
和此回溯:
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>)/$']
您需要:
更改详细信息视图的上下文以仅访问单个种子,还包括URL中的参数:
details(request, torrent_hash):
return render_to_response('mysite/detail_torrent.html', {'torrent':Torrent.objects.filter(hash=torrent_hash)})
使用这样的URL,它通过您的torrent哈希传递到视图:
url(r'^torrents/(?P<torrent_hash>)/$', 'details', name = 'hash'),
您还需要您的detail_torrent.html模板,然后可以在其中使用“ torrent”上下文。
编辑以抢占另一个问题:
在您的主模板中,您可以使用此更改链接到种子。然后,您将torrent.Hash变量作为参数传递给您的URL,该变量将用于URL正则表达式中的torrent_hash:
{% for torrent in torrents %}
<p id="demo">
<tr bgcolor=eeeeee>
<td width=560 height=20><a href="{% url 'hash' torrent.Hash %}">{{ torrent.Hash }}</a></td>
<td width=710 height=20>{{ torrent.Name }}</td>
<td width=110 height=20>{{ torrent.Size }}</td>
<td width=110 height=20></td>
</tr>
</p>
{% endfor %}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句