Django:如何在单击一行时更改页面?

布10

当我在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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

单击一行时添加图像(<li>标签)

来自分类Dev

更改一行时的Django内部服务器错误

来自分类Dev

单击表行时突出显示,并在单击下一行时禁用

来自分类Dev

单击custome listview中的按钮时,如何更改特别是一行textview的值?

来自分类Dev

HTML + JavaScript:如何在单击Javascript中的按钮时突出显示一行?

来自分类Dev

单击装订线左侧时如何在 SynEdit 中选择一行?

来自分类Dev

当行中的一项被重复并且仅显示一行时,如何在行中添加星号

来自分类Dev

如何在按钮单击上添加一行?

来自分类Dev

在VB中的DataGridView中单击一行时填充文本框?

来自分类Dev

Xamarin Forms MVVM在CollectionView中单击一行时显示图标/图像

来自分类Dev

单击动态生成的表中的一行时,选中其单选按钮

来自分类Dev

在VB中的DataGridView中单击一行时填充文本框?

来自分类Dev

单击时从JTextArea获取一行

来自分类Dev

当元素滚动超过一行时更改元素的 CSS 属性

来自分类Dev

当我使用间接选择在dojo的增强型网格中单击一行时如何自动选中复选框

来自分类Dev

我如何将鼠标指针更改为手势并在鼠标移过表格中的一行时更改行的颜色

来自分类Dev

当表中的一行不适合页面时,如何在TCPDF上设置自动分页符?

来自分类Dev

如何在单击事件时在运行时更改html按钮的类?

来自分类Dev

如何在单击事件时在运行时更改html按钮的类?

来自分类Dev

当另一行单击时,切换按钮向后更改

来自分类Dev

如何在模型中雄辩地在laravel中插入一行时如何获取最后一个id

来自分类Dev

当我在第100行时,如何显示第一行?

来自分类Dev

LibreOffice Writer:当表位于第二行时,如何将其排成一行?

来自分类Dev

找到一行时如何从文件中获取下n行

来自分类Dev

如何在文本悬停时创建一行?

来自分类Dev

当输入与 Django 中的一行匹配时,如何更改现有数据库中的值

来自分类Dev

IDE的问题,当您在编辑器中单击一行时,光标不会移至最后写入的字符

来自分类Dev

仅在DataGrid中选择一行时如何启用按钮?

来自分类Dev

每次创建一行时如何运行SQL语句?

Related 相关文章

  1. 1

    单击一行时添加图像(<li>标签)

  2. 2

    更改一行时的Django内部服务器错误

  3. 3

    单击表行时突出显示,并在单击下一行时禁用

  4. 4

    单击custome listview中的按钮时,如何更改特别是一行textview的值?

  5. 5

    HTML + JavaScript:如何在单击Javascript中的按钮时突出显示一行?

  6. 6

    单击装订线左侧时如何在 SynEdit 中选择一行?

  7. 7

    当行中的一项被重复并且仅显示一行时,如何在行中添加星号

  8. 8

    如何在按钮单击上添加一行?

  9. 9

    在VB中的DataGridView中单击一行时填充文本框?

  10. 10

    Xamarin Forms MVVM在CollectionView中单击一行时显示图标/图像

  11. 11

    单击动态生成的表中的一行时,选中其单选按钮

  12. 12

    在VB中的DataGridView中单击一行时填充文本框?

  13. 13

    单击时从JTextArea获取一行

  14. 14

    当元素滚动超过一行时更改元素的 CSS 属性

  15. 15

    当我使用间接选择在dojo的增强型网格中单击一行时如何自动选中复选框

  16. 16

    我如何将鼠标指针更改为手势并在鼠标移过表格中的一行时更改行的颜色

  17. 17

    当表中的一行不适合页面时,如何在TCPDF上设置自动分页符?

  18. 18

    如何在单击事件时在运行时更改html按钮的类?

  19. 19

    如何在单击事件时在运行时更改html按钮的类?

  20. 20

    当另一行单击时,切换按钮向后更改

  21. 21

    如何在模型中雄辩地在laravel中插入一行时如何获取最后一个id

  22. 22

    当我在第100行时,如何显示第一行?

  23. 23

    LibreOffice Writer:当表位于第二行时,如何将其排成一行?

  24. 24

    找到一行时如何从文件中获取下n行

  25. 25

    如何在文本悬停时创建一行?

  26. 26

    当输入与 Django 中的一行匹配时,如何更改现有数据库中的值

  27. 27

    IDE的问题,当您在编辑器中单击一行时,光标不会移至最后写入的字符

  28. 28

    仅在DataGrid中选择一行时如何启用按钮?

  29. 29

    每次创建一行时如何运行SQL语句?

热门标签

归档