Django - 将视图中的变量传递给模板中的URL标签 [英] Django - passing a variable from a view into a url tag in template

查看:121
本文介绍了Django - 将视图中的变量传递给模板中的URL标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,对于noobish的问题,我深表歉意。我对Django很新,我相信我缺少一些明显的东西。我已经读了很多其他的帖子,还没有找到任何明显的事情我做错了。非常感谢任何帮助,我在一个截止日期。

First of all, I apologize for the noobish question. I'm very new to Django and I'm sure I'm missing something obvious. I have read many other posts here and have not been able to find whatever obvious thing I am doing wrong. Thanks so much for any help, I am on a deadline.

我正在使用Django 1.6与Python 2.7。我有一个名为dbquery的应用程序,使用表单从用户处获取数据并查询REST服务。然后我尝试在结果页面上显示结果。
显然有更多的添加,这只是一个非常简单的开始。

I am using Django 1.6 with Python 2.7. I have one app called dbquery that uses a form to take data from the user and query a REST service. I am then trying to display the results on a results page. Obviously there is more to add, this is just a very simple start.

问题是我似乎无法将我的搜索视图中的自动增量id字段正确地放入模板中的url标签。如果我把数字1放在这个 {%url'dbquery:results'search_id = 1%} 中,页面加载并且工作得很好,但我似乎不能获取变量名称,django文档没有帮助 - 这可能对大多数人来说是显而易见的。我得到一个反向错误,因为变量最终总是空的,所以它不能匹配我的urls.py中的正则表达式结果。我测试了我的代码在命令行shell中添加一个对象,并且它似乎工作。在我看来,我的return render()语句有问题吗?

The problem is that I can't seem to get the autoincremented id field from my search view into the url tag in the template properly. If I put the number 1 in like this {% url 'dbquery:results' search_id=1 %}, the page loads and works well, but I can't seem to get the variable name right, and the django documentation isn't helping- maybe this is obvious to most people. I get a reverse error because the variable ends up always being empty, so it can't match the results regex in my urls.py. I tested my code for adding an object in the command line shell and it seems to work. Is there a problem with my return render() statement in my view?

urls.py

from django.conf.urls import patterns, url
from dbquery import views

urlpatterns = patterns('',

    # ex: /search/
    url(r'^$', views.search, name='search'),

    # ex: /search/29/results/ --shows response from the search
    url(r'^(?P<search_id>\d+)/results/', views.results, name ='results'),
)

models.py

models.py

from django.db import models
from django import forms
from django.forms import ModelForm
import datetime

# response data from queries for miRNA accession numbers or gene ids
class TarBase(models.Model):
    #--------------miRNA response data----------
    miRNA_name = models.CharField('miRNA Accession number', max_length=100)
    species = models.CharField(max_length=100, null=True, blank=True)
    ver_method = models.CharField('verification method', max_length=100, null=True, blank=True)
    reg_type = models.CharField('regulation type', max_length=100, null=True, blank=True)
    val_type = models.CharField('validation type', max_length=100, null=True, blank=True)
    source = models.CharField(max_length=100, null=True, blank=True)
    pub_year = models.DateTimeField('publication year', null=True, blank=True)
    predict_score = models.DecimalField('prediction score', max_digits=3, decimal_places=1, null=True, blank=True)
    #gene name  
    gene_target = models.CharField('gene target name',max_length=100, null=True, blank=True)
    #ENSEMBL id
    gene_id = models.CharField('gene id', max_length=100, null=True, blank=True)
    citation = models.CharField(max_length=500, null=True, blank=True)

    def __unicode__(self):  
        return unicode(str(self.id) + ": " + self.miRNA_name) or 'no objects found!'

views.py <从django.shortcuts导入render,get_object_or_404
从django.http导入HttpResponse,Http404,HttpResponseRedirect
从django p>

views.py

from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.core.urlresolvers import reverse
from dbquery.models import TarBase, SearchMainForm
from tarbase_request import TarBaseRequest

#main user /search/ form view
def search(request):
    if request.method == 'POST': #the form has been submitted
        form = SearchMainForm(request.POST) #bound form
        if form.is_valid(): #validations have passed
            miRNA = form.cleaned_data['miRNA_name']

            u = TarBase.objects.create(miRNA_name=miRNA)

            #REST query will go here.

            #commit to database
            u.save()

            return render(request,'dbquery/results.html', {'id':u.id})

    else: #create an unbound instance of the form
        form = SearchMainForm(initial={'miRNA_name':'hsa-let-7a-5p'})
    #render the form according to the template, context = form
    return render(request, 'dbquery/search.html', {'form':form})


#display results page: /search/<search_id>/results/ from requested search
def results(request, search_id):
    query = get_object_or_404(TarBase, pk=search_id)
    return render(request, 'dbquery/results.html', {'query':query} )

templates:
search.html

templates: search.html

<html>
<head><h1>Enter a TarBase Accession Number</h1>
</head>
<body>
<!--form action specifies the next page to load-->
<form action="{% url 'dbquery:results' search_id=1 %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Search" />
</form>

</body>

results.html

results.html

<html>
<head><h1>Here are your results</h1>
</head>
<body>
{{query}}

</body


推荐答案

搜索结果未创建,直到提交表单后才能使用ID。通常的方法是使您的表单使用自己的URL作为操作,然后在成功保存后将视图重定向到结果视图:

The search results aren't created and don't have an ID until after you submit your form. The usual way to do this would be to have your form use its own URL as the action, then have the view redirect to the results view after successfully saving:

from django.shortcuts import redirect

def search(request):
    if request.method == 'POST': #the form has been submitted
        form = SearchMainForm(request.POST) #bound form
        if form.is_valid(): #validations have passed
            miRNA = form.cleaned_data['miRNA_name']
            u = TarBase.objects.create(miRNA_name=miRNA)
            #REST query will go here.

            #commit to database
            u.save()

            return redirect('results', search_id=u.id)

    else: #create an unbound instance of the form
        form = SearchMainForm(initial={'miRNA_name':'hsa-let-7a-5p'})
    #render the form according to the template, context = form
    return render(request, 'dbquery/search.html', {'form':form})

然后在你的模板中:

<form action="" method="post">

这将导致您的表单将其数据提交到搜索验证视图。如果表单有效,则视图保存结果,然后根据保存后确定的ID重定向到相应的结果页。

That causes your form to submit its data to the search view for validation. If the form is valid, the view saves the results, then redirects to the appropriate results page based on the ID as determined after saving.

这篇关于Django - 将视图中的变量传递给模板中的URL标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆