自引用模块的 Django URL 中的正则表达式
Regular expression in django urls for self referenced module
我必须在我的 django urls 中写什么正则表达式到 simulate,在 url 文件路径中?其中有 "directories"、"subdirectories" 和 "files",其中每个对象在模型中都有一个 "slug"。
我已经这样做了:
model.py
class Directory(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(max_length=100, unique=True,)
subdirectory = models.ForeignKey('self', null=True, blank=True)
class File(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(max_length=100, unique=True,)
directory = models.ForeignKey(Directory)
views.py
class DirectoryView(generic.DetailView):
model = Directory
template_name = 'appname/directory.html'
class FileView(generic.DetailView):
model = File
template_name = 'appname/file.html'
appname/urls.py
urlpatterns = [
url(r'^(?P<slug>[-\w]+)/$',
views.DirectoryView.as_view(), name='directory'),
url(r'^(?P<slug1>[-\w]+)/(?P<slug>[-\w]+)/$',
views.FileView.as_view(), name='file'),
]
"file1.txt" 的结果 url 是:
- 文件夹1
- 文件夹2
- 子文件夹1
- file1.txt
http://mydomain.com/appname/subfolder1/file1txt
但我需要它是:
http:/mydomain.com/appname/folder2/subfolder1/file1txt
谢谢,
伊万
所以我终于在这个 Django 片段中找到了答案:
https://djangosnippets.org/snippets/362/
这是我最后得到的:
models.py
class Directory(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(max_length=100, unique=True,)
parent = models.ForeignKey('self', null=True, blank=True)
def get_absolute_url(self):
url = "/%s/" % self.slug
cwd = self
while cwd.parent:
url = "/%s%s" % (cwd.parent.slug, url)
cwd = cwd.parent
return url
class File(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(max_length=100, unique=True,)
directory = models.ForeignKey(Directory)
views.py
from django.http import Http404
from django.shortcuts import render, get_object_or_404
from .models import Archive, Directory, File
def directories(request, full_slug):
slugs = full_slug.split('/')
cwd_slug = slugs[-1]
directory = get_object_or_404(Directory, slug=cwd_slug)
if not directory.get_absolute_url().strip('/') == full_slug:
raise Http404
# if you reach this line, you've found the directory
context = {'directory': directory}
template = 'appname/directory.html'
return render(request, template, context)
def files(request, full_slug, file_slug):
slugs = full_slug.split('/')
cwd_slug = slugs[-1]
directory = get_object_or_404(Directory, slug=cwd_slug)
if not directory.get_absolute_url().strip('/') == full_slug:
raise Http404
files = get_object_or_404(File, name=file_slug)
# if you reach this line, you've found the file
context = {'archive': archive_slug, 'directory': full_slug, 'file': files}
template = 'getdocs/file.html'
return render(request, template, context)
appname/urls.py
from django.conf.urls import url, include
from . import views
file_patterns = [
url(r'^$', views.directories, name='directory'),
url(r'^(?P<file_slug>(.*)[.]{1}[-\w]{3})$', views.files, name='file'),
]
urlpatterns = [
url(r'^(?P<full_slug>(.*))/', include(file_patterns)),
]
我希望这对其他人有帮助。
我必须在我的 django urls 中写什么正则表达式到 simulate,在 url 文件路径中?其中有 "directories"、"subdirectories" 和 "files",其中每个对象在模型中都有一个 "slug"。
我已经这样做了:
model.py
class Directory(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(max_length=100, unique=True,)
subdirectory = models.ForeignKey('self', null=True, blank=True)
class File(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(max_length=100, unique=True,)
directory = models.ForeignKey(Directory)
views.py
class DirectoryView(generic.DetailView):
model = Directory
template_name = 'appname/directory.html'
class FileView(generic.DetailView):
model = File
template_name = 'appname/file.html'
appname/urls.py
urlpatterns = [
url(r'^(?P<slug>[-\w]+)/$',
views.DirectoryView.as_view(), name='directory'),
url(r'^(?P<slug1>[-\w]+)/(?P<slug>[-\w]+)/$',
views.FileView.as_view(), name='file'),
]
"file1.txt" 的结果 url 是:
- 文件夹1
- 文件夹2
- 子文件夹1
- file1.txt
- 子文件夹1
http://mydomain.com/appname/subfolder1/file1txt
但我需要它是:
http:/mydomain.com/appname/folder2/subfolder1/file1txt
谢谢,
伊万
所以我终于在这个 Django 片段中找到了答案:
https://djangosnippets.org/snippets/362/
这是我最后得到的:
models.py
class Directory(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(max_length=100, unique=True,)
parent = models.ForeignKey('self', null=True, blank=True)
def get_absolute_url(self):
url = "/%s/" % self.slug
cwd = self
while cwd.parent:
url = "/%s%s" % (cwd.parent.slug, url)
cwd = cwd.parent
return url
class File(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(max_length=100, unique=True,)
directory = models.ForeignKey(Directory)
views.py
from django.http import Http404
from django.shortcuts import render, get_object_or_404
from .models import Archive, Directory, File
def directories(request, full_slug):
slugs = full_slug.split('/')
cwd_slug = slugs[-1]
directory = get_object_or_404(Directory, slug=cwd_slug)
if not directory.get_absolute_url().strip('/') == full_slug:
raise Http404
# if you reach this line, you've found the directory
context = {'directory': directory}
template = 'appname/directory.html'
return render(request, template, context)
def files(request, full_slug, file_slug):
slugs = full_slug.split('/')
cwd_slug = slugs[-1]
directory = get_object_or_404(Directory, slug=cwd_slug)
if not directory.get_absolute_url().strip('/') == full_slug:
raise Http404
files = get_object_or_404(File, name=file_slug)
# if you reach this line, you've found the file
context = {'archive': archive_slug, 'directory': full_slug, 'file': files}
template = 'getdocs/file.html'
return render(request, template, context)
appname/urls.py
from django.conf.urls import url, include
from . import views
file_patterns = [
url(r'^$', views.directories, name='directory'),
url(r'^(?P<file_slug>(.*)[.]{1}[-\w]{3})$', views.files, name='file'),
]
urlpatterns = [
url(r'^(?P<full_slug>(.*))/', include(file_patterns)),
]
我希望这对其他人有帮助。