やりたいこと
ディレクトリ内のフォルダ・ファイルを取得してそれを表示し、それぞれに、次のディレクトリへのリンクを張りたいと考えています。つまり
bash
1$ tree 2a 3|__ b 4|__ c 5d 6|__ e 7
というような階層構造があったときに、ページにはaとdが表示されていて、aをクリックするとディレクトリa内部に移動してb, cが表示される、というようなことを実現したいと思っています。
現状
現時点で、ルートディレクトリのフォルダ・ファイルを取得してページ上に表示するところまでは実現しているのですが、リンクを張ることができていません。
階層構造としては、次のようになっています。メインがconfigと名前がついているもので、ここに新たにfile_choiceというアプリケーションが加わっている状況です。
bash
1. 2├── config 3│ ├── __init__.py 4│ ├── __pycache__ 5│ │ ├── __init__.cpython-36.pyc 6│ │ ├── settings.cpython-36.pyc 7│ │ ├── urls.cpython-36.pyc 8│ │ └── wsgi.cpython-36.pyc 9│ ├── settings.py 10│ ├── urls.py 11│ └── wsgi.py 12├── db.sqlite3 13├── file_choice 14│ ├── admin.py 15│ ├── apps.py 16│ ├── __init__.py 17│ ├── migrations 18│ │ ├── __init__.py 19│ │ └── __pycache__ 20│ │ └── __init__.cpython-36.pyc 21│ ├── models.py 22│ ├── __pycache__ 23│ │ ├── admin.cpython-36.pyc 24│ │ ├── apps.cpython-36.pyc 25│ │ ├── __init__.cpython-36.pyc 26│ │ ├── models.cpython-36.pyc 27│ │ ├── urls.cpython-36.pyc 28│ │ ├── views.cpython-35.pyc 29│ │ └── views.cpython-36.pyc 30│ ├── static 31│ │ └── file_choice 32│ │ └── style.css 33│ ├── templates 34│ │ └── file_choice 35│ │ └── index.html 36│ ├── tests.py 37│ ├── urls.py 38│ └── views.py 39├── libs 40│ ├── dir_check.py 41│ ├── __init__.py 42│ └── __pycache__ 43│ ├── dir_check.cpython-35.pyc 44│ ├── dir_check.cpython-36.pyc 45│ ├── __init__.cpython-35.pyc 46│ └── __init__.cpython-36.pyc 47├── lib_tests 48│ └── test_dir_check.py 49└── manage.py
config内のurls.py
は
python
1from django.contrib import admin 2from django.urls import path, include 3from django.conf.urls import url 4 5urlpatterns = [ 6 path('file_choice/', include('file_choice.urls', namespace='file_choice')), 7 path('admin/', admin.site.urls), 8]
file_choice内のファイルは
urls.pyが
python
1from django.urls import path 2 3from . import views 4 5app_name = 'file_choice' 6urlpatterns = [ 7 path('', views.DirectoryChange.as_view(), name='index'), 8 path('file_choice/<str:next>/', views.DirectoryChange.as_view(), name='index'), 9 ]
views.pyが
python
1from libs.dir_check import DirCheck 2 3from django.shortcuts import render 4from django.template import loader 5from django.http import HttpResponse 6from django.views import generic 7 8# Create your views here. 9class DirectoryChange(generic.TemplateView): 10 template_name = 'file_choice/index.html' 11 12 def get_context_data(self, **kwargs): 13 context = super().get_context_data(**kwargs) 14 context['dir_info'] = DirCheck().get_info() 15 16 return context
そして、template/file_choice/index.htmlが
html
1<!DOCTYPE html> 2<head> 3 <meta charset="utf-8"> 4 {% load static %} 5 <link rel="stylesheet" type="text/css" href="{% static 'file_choice/style.css' %}"> 6 <title>files</title> 7</head> 8<body> 9 <h1>Files</h1> 10 <div class="file_table"> 11 <a href=""> <!-- ここに何を書くべきか --> 12 <div class="files"> 13 .. 14 </div> 15 </a> 16 {% for f in dir_info.next %} 17 <a href=""> <!-- ここに何を書くべきか --> 18 <div class="files"> 19 {{ f }} 20 </div> 21 </a> 22 {% endfor %} 23 </div> 24</body>
そして、DirCheckというクラスは自分で定義していて、現在のディレクトリとその中のフォルダ・ファイル、そして親ディレクトリを保持しています。
python
1import os 2 3class DirCheck: 4 def __init__(self): 5 self.now = self.get_now_dir() 6 self.previous = self.previous_dir() 7 self.next = self.next_dirs() 8 9 def get_now_dir(self): 10 return '/mnt/c/' 11 12 def next_dirs(self): 13 return os.listdir(self.now) 14 15 def previous_dir(self): 16 previous = self.now.rsplit('/', 2)[0] + '/' 17 return previous 18 19 def move_forward(self, to): 20 temp_now = self.now 21 self.now += to 22 self.previous = temp_now 23 self.next = self.next_dirs() 24 return self.now 25 26 def move_backward(self): 27 temp_now = self.now 28 self.now = self.previous 29 self.next = temp_now 30 self.previous = self.previous_dir() 31 32 def get_info(self): 33 context = { 34 'now' : self.now, 35 'next' : self.next, 36 'previous' : self.previous, 37 } 38 39 return context
Django初心者で、わからないことが多いのですがよろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。