python - 为反向 View 传递 kwargs; Django 1.5

标签 python django

作为一个django初学者,我有一个小问题要处理:

html代码:

<li><a href="{% url 'dyn_display' category='first'%}">first</a></li>
<li><a href="{% url 'dyn_display' category='second'%}">second</a></li>
<li><a href="{% url 'dyn_display' category='third'%}">third</a></li>

urlconf.py

url(r'(?P<category>[a-z]+)$', 'display', name='dyn_display')

View .py

def courses_display(request, category):
    return render_to_response('display/basic.html', {category: 'in'}, context_instance=RequestContext(request))

最后是 basic.html 的一部分,所以你可以了解为什么我需要那个类别变量

basic.html

<div class="accordion" id="accordion2">
    <div class="accordion-group">
        <div class="accordion-heading">
            <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseFirst">
                <strong>First</strong>
            </a>
        </div>
        <div id="collapseFirst" class="accordion-body collapse {{ first }}">
             <div class="accordion-inner">
                 ...
             </div>
         </div>
     </div>
     <div class="accordion-group">
         <div class="accordion-heading">
             <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseSecond">
                 <strong>Second</strong>
             </a>
         </div>
         <div id="collapseSecond" class="accordion-body collapse {{ second }}">
             <div class="accordion-inner">
                 ...
             </div>
         </div>
     </div>
     <div class="accordion-group">
         <div class="accordion-heading">
             <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseThird">
                 <strong>Third</strong>
             </a>
         </div>
         <div id="collapseThird" class="accordion-body collapse {{ third }}">
             <div class="accordion-inner">
                 ...
             </div>
         </div>
      </div>
  </div>

因为整个页面都是用bootstrap和Jquery完成的,url的最后部分

some/url/first

一些/url/第二个

some/url/third

有点难看,因为 Accordion 元素让我无需重新加载页面即可折叠并打开相应的内部主体。所以问题是:有没有办法反向传递一个 kwarg,它将在 View 中使用,而不是确定 reverse() 中的 url?

我获取像 some/url/ 这样的 url 并且仍然将 kwarg 从第一个 .html 页面传递到 View 的方式?

最佳答案

你可以使用这个:

如果这是您的网址:

url(r'(?P<category>[a-z]+)$', 'display', name='dyn_display')
reverse('dyn_display', kwargs={'category': 'first'})

要重定向,您可以在您的 View 中像这样使用它:

from django.http import HttpResponseRedirect
return HttpResponseRedirect(reverse('dyn_display', kwargs={'category': 'first'}))

如果这是您的网址:

url(r'$', 'display', name='dyn_dysplay')
reverse('dyn_display')

要重定向,您可以在您的 View 中像这样使用它:

from django.http import HttpResponseRedirect
return HttpResponseRedirect(reverse('dyn_display'))

要有一个可以接收可选值的 View ,您需要 2 个 url:

url(r'$', 'display', name='dyn_optional_display')    
url(r'(?P<category>[a-z]+)$', 'display', name='dyn_display')

然后是你的观点:

def courses_display(request, category=None):
    ctx = {}
    if category:
        ctx.update({category: 'in'})
    return render_to_response('display/basic.html', ctx,    context_instance=RequestContext(request))

关于python - 为反向 View 传递 kwargs; Django 1.5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15153570/

相关文章:

javascript - 为 Django 应用编写 JS 测试

python - 从 Tensorflow 数据集中分割数据时出现问题

python - python字典异步安全吗?

python - 在 Django 中本地化的数字格式?

javascript - Django:如何使用模板从静态文件加载 javascript

python - 在django admin中使用代理模型自定义更改 ListView

python - 使用 Django 的 reportlab header 参数

python - 使用mlab在mayavi中用文本注释许多点

python - 如何在 Pandas 中进行分组

python - 将变量与字典中的第二级进行匹配