Reputation: 5587
I am working on Django framework. I am searching for function which call view from template.
In Asp.NET MVC we can call action(view) from view(template) in this way.
@Html.Action("Controller Name", "Action Name") or
@Html.Action("Action Name")
In simple words I want to get template html in other template.
def index(request):
return render(request, 'index.html')
def my_list(request):
q = MyModel.objects.all()
return render(request, 'mylist.html',{'my_list':q})
index.html
<html>
<head></head>
<body>
#want to call my_list view
</body>
</html>
mylist.html
<script>
#lot of script here
</script>
{% for record in my_list %}
<p>{{ record }}</p>
{% endfor %}
The solution which i have get from my search use html form or jquery post or get request.
my_list view contains queryset objects. I dont want call this query directly in index view. This is a partial view.
What are the solution are available for this type of issue?
Thank you
Upvotes: 0
Views: 806
Reputation: 774
Use include
tag.
You can pass context to the template using with
statement.
This isn't the same as @Html.Action
, but it's quite similar.
https://docs.djangoproject.com/en/1.7/ref/templates/builtins/#include
Upvotes: 1
Reputation: 45555
It is doable but seems like you are trying to reinvent inclusion template tags.
@register.inclusion_tag('mylist.html')
def my_list():
q = MyModel.objects.all()
return {'my_list': q}
And then in index.html
:
<html>
{% load my_tags %}
<head></head>
<body>
{% my_list %}
</body>
</html>
Upvotes: 2