Hi!

I am working on my first Django project. I read its entire documentation 
and searched both  this group and stackoverflow for an answer to my 
question and I think what I want can't be achieved, so I'm just posting 
this hoping to be wrong and missing something.

I have an index.html template, which gets from its view context a list of 
objects from the database to build a dynamic list. This template is then 
used as the base template for a couple of others. So, my views which render 
the templates that extend the index must also have my list on their context 
in order to build the index base list? If I don't pass my list variable in 
the context, my base list is empty. I know http requests are stateless and 
from what I've got from template inheritance, it's a way to compose a 
complex layout without repeating code, but in order to make my dynamic list 
work in my base index template, I have to either:

1 - pass my list variable to every template that extends the base one;
2 - extend HttpRequest to always load my list

Do you guys have any other suggestion? Am I doing something wrong? Thank 
you very much in advance. Below is the code from my example:


*# views.py:*

def index(request):
    datasets = get_list_or_404(DataSet, user=request.user)
    context = { 'datasets': datasets }
    return render(request, 'datasets/index.html', context)

def detail(request, pk):
    ds = get_object_or_404(DataSet, pk=pk)
    *datasets = get_list_or_404(DataSet, user=request.user)* *# passing it 
to context to be loaded in the index template*
    context = { 'dataset': ds, 'datasets': datasets }
    return render(request, 'datasets/detail.html', context)


*# index.html:*

{% extends 'base.html' %}
{% block title %}Datasets{% endblock %}
{% block content %}
<div id="sidebar">
  <h1>Datasets</h1>
  <ul>
  {% for ds in datasets %}
    <li><a href="{% url 'detail' ds.id %}">{{ ds.name }}</a></li>
  {% endfor %}
  </ul>
</div>
<div id="ds_content">
{% block ds_content %}
  <a href="{% url 'admin:mlweb_dataset_add' %}">Add dataset</a>
{% endblock %}
</div>
{% endblock %}


*# detail.html:*

{% extends 'datasets/index.html' %}
{% block title %}{{dataset.name}}{% endblock %}
{% block ds_content %}
<h1>{{ dataset.name }}</h1>
<a href="{% url 'admin:mlweb_dataset_change' dataset.id %}">Edit</a>
<a href="{% url 'admin:mlweb_dataset_delete' dataset.id %}">Delete</a>
<a href="{% url 'train' dataset.id %}">Train</a>
<a href="{% url 'predict' dataset.id %}">Predict</a>
{% endblock %}

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/75d0dfec-dcd0-4938-8e19-74daa12602d3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to