如何在Django模板中显示过滤的对象?

时间:2023-01-27 16:05:29

Say I have an Model which displays as follows.

假设我有一个显示如下的模型。

class Opt(models.Model):
    optID = models.CharField()
    availabiltities = models.ManyToManyField(Available)
    lastdatemodified = models.DateTimeField()
    ....
    ....

Also, opt_ID is not the primary_key

此外,opt_ID不是primary_key

I want to be able to display all the field with respect to that opt_id in the html template. I have a page which displays all the Opt_id for that model. And each Opt_ID is a link which is supposed to display all the fields as I mentioned before...

我希望能够在html模板中显示与opt_id相关的所有字段。我有一个页面显示该模型的所有Opt_id。每个Opt_ID都是一个链接,应该显示我之前提到的所有字段......

In views.py I am currently writing,

在views.py我正在写,

def Optschedule(request, opt_id = 1):
   return render_to_response('show.html', {'OptSchedule', Opt.objects.filter(optID=Opt_id)})

In show.html

在show.html中

{% for entries in OptSchedule %}
    {{entries.lastdatemodified}}
    {{entries.availabilities}}
{% endfor %}

I guess, i am not understanding how do we pass arguments to the view function so that it uses those arguments for the queryset in views.

我想,我不知道如何将参数传递给视图函数,以便它在视图中使用这些参数作为查询集。

Thanks.

谢谢。

1 个解决方案

#1


0  

I understood your question, but you're missing some basics of Django

我理解你的问题,但你遗漏了一些Django的基础知识

You can pass the arguments to your view in your request, configuring properly your urlconf.

您可以在请求中将参数传递给视图,正确配置您的urlconf。

But first, let's use lowercase for function names, as preconized by Python PEP8:

但首先,让我们使用小写的函数名称,由Python PEP8预先规划:

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

函数名称应为小写,并根据需要用下划线分隔,以提高可读性。

def opt_schedule(request, opt_id):
    opt_schedule = Opt.objects.get(id=opt_id)
    return render_to_response('show.html', {'OptSchedule': opt_schedule})

You're passing opt_id to your view, and this opt_id came from your urlconf, suppose:

您正在将opt_id传递给您的视图,而这个opt_id来自您的urlconf,假设:

(r'^show/(\d+)/$', opt_schedule),

this urlconf says that you're passing a decimal integer for your view opt_schedule, besides the request object. When you call www.example.com/show/1/, the view opt_schedule is called and receive request object and the decimal 1. Inside your view function, you're querying for Opt object that have the id (or pk) = 1, and passes it to your context with the name 'OptSchedule'

这个urlconf说除了请求对象之外,你还为视图opt_schedule传递了一个十进制整数。当您调用www.example.com/show/1/时,将调用视图opt_schedule并接收请求对象和小数1.在查看函数内,您要查询具有id(或pk)= 1的Opt对象,并将其传递给您的上下文,名称为“OptSchedule”

Now you can access your object attributes in your template, like:

现在,您可以在模板中访问对象属性,例如:

{{ OptSchedule.lastdatemodified }}

#1


0  

I understood your question, but you're missing some basics of Django

我理解你的问题,但你遗漏了一些Django的基础知识

You can pass the arguments to your view in your request, configuring properly your urlconf.

您可以在请求中将参数传递给视图,正确配置您的urlconf。

But first, let's use lowercase for function names, as preconized by Python PEP8:

但首先,让我们使用小写的函数名称,由Python PEP8预先规划:

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

函数名称应为小写,并根据需要用下划线分隔,以提高可读性。

def opt_schedule(request, opt_id):
    opt_schedule = Opt.objects.get(id=opt_id)
    return render_to_response('show.html', {'OptSchedule': opt_schedule})

You're passing opt_id to your view, and this opt_id came from your urlconf, suppose:

您正在将opt_id传递给您的视图,而这个opt_id来自您的urlconf,假设:

(r'^show/(\d+)/$', opt_schedule),

this urlconf says that you're passing a decimal integer for your view opt_schedule, besides the request object. When you call www.example.com/show/1/, the view opt_schedule is called and receive request object and the decimal 1. Inside your view function, you're querying for Opt object that have the id (or pk) = 1, and passes it to your context with the name 'OptSchedule'

这个urlconf说除了请求对象之外,你还为视图opt_schedule传递了一个十进制整数。当您调用www.example.com/show/1/时,将调用视图opt_schedule并接收请求对象和小数1.在查看函数内,您要查询具有id(或pk)= 1的Opt对象,并将其传递给您的上下文,名称为“OptSchedule”

Now you can access your object attributes in your template, like:

现在,您可以在模板中访问对象属性,例如:

{{ OptSchedule.lastdatemodified }}