2👍
To add content to your DetailView
override the get_context_data
method. For instance, if you add the following method to your ProjectDetailView
class:
def get_context_data(self, **kwargs):
context = super(ProjectDetailView, self).get_context_data(**kwargs)
context['hello'] = "Hello, world"
return context
you will have an extra context variable named hello
in your template, so you would output it with {{ hello }}
.
Hint: CBV Inspector is your friend when you deal with CBVs.
Update
OP wanted to pass the directory django is running from to his traverse_dir
function. To do that, you can add the following to your settings.py
(django 1.6 adds it by default):
import os BASE_DIR = os.path.dirname(os.path.dirname(__file__))
Now, you can change your get_context_path
method of ProjectDetailView
like this:
from django.conf import settings
def get_context_data(self, **kwargs):
context = super(ProjectDetailView, self).get_context_data(**kwargs)
context['dirs'] = traverse_dir(settings.BASE_PATH)
return context
Now you will have a dirs
variable in your context which can be outputed using (for instance) a {% for %}
loop.
Notice: I haven’t checked if traverse_dir
is working as expected.
Update 2
It turns out the OP had a different question: How to display a folder hierarchy with django. What I’d do is create simple view (not DetailView) named traverse
and add the following url patterns in my urls.py
url(r'^traverse/(.+)*', 'views.test.traverse', name='traverse' ),
Now, the traverse
view could be implemented like this:
def traverse(request, segments=""):
segments = segments.split(r'/')
# segments is an array of the url paths
# construct the path of the folder you want to view here
# by concatenate settings.BASE_PATH with the url components
# finally output the contents of the folder by creating links
# which concatenate the segments with the contents of the folder