django 模板文件路径设置

TEMPLATE 默认配置

settings.py:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
TEMPLATES  =  [
     {
         'BACKEND' 'django.template.backends.django.DjangoTemplates' ,
         'DIRS' : [os.path.join(BASE_DIR,  'templates' )],
         'APP_DIRS' True ,
         'OPTIONS' : {
             'context_processors' : [
                 'django.template.context_processors.debug' ,
                 'django.template.context_processors.request' ,
                 'django.contrib.auth.context_processors.auth' ,
                 'django.contrib.messages.context_processors.messages' ,
             ],
         },
     },
]

BACKEND:模板引擎类的python路径,内置的模板引擎分别有'django.template.backends.django.DjangoTemplates'和'django.template.backends.jinja2.Jinja2'

DIRS:模板引擎搜索模板的路径,如上,默认搜索project目录下的templates目录

APP_DIRS:告诉模板引擎是否搜索app目录下的templates目录。默认为true,即是默认搜索app目录下的templates目录  

'DIRS': [os.path.join(BASE_DIR, 'templates')]  是指到  BASE_DIR/templates文件夹中去取模板
'DIRS': [os.path.join(BASE_DIR, 'app1/templates')] 是指到  BASE_DIR/app1/templates文件夹中去取模板


模板文件查找顺序

如果同时设置

'DIRS' : [os.path.join(BASE_DIR,  'templates' )],
'APP_DIRS' True ,

并且同时存在

mysite\templates\polls\index.html

mysite\polls\templates\polls\index.html

则会访问 mysite\templates\polls\index.html 而不是 mysite\polls\templates\polls\index.html


模板文件命名空间

First, create a directory called templates in your polls directory. Django will look for templates in there.

Your project’s TEMPLATES setting describes how Django will load and render templates. The default settings file configures aDjangoTemplates backend whose APP_DIRS option is set to True. By convention DjangoTemplates looks for a “templates” subdirectory in each of the INSTALLED_APPS.

Within the templates directory you have just created, create another directory called polls, and within that create a file calledindex.html. In other words, your template should be at polls/templates/polls/index.html. Because of how theapp_directories template loader works as described above, you can refer to this template within Django simply aspolls/index.html.

Template namespacing

Now we might be able to get away withputting our templates directly in polls/templates (rather than creating anotherpolls subdirectory), but it would actually be a bad idea.Django will choose the first template it finds whose name matches, and if you had a template with the same name in a different application, Django would be unable to distinguish between them. We need to be able to point Django at the right one, and the easiest way to ensure this is by namespacing them. That is, by putting those templates inside another directory named for the application itself.

xys友情提醒:

模板文件的命名空间(其实质是模板文件的本地相对路径)的应用场景是,在每个APP下建立appname/templates/appname/,然后在该目录下再放模板文件(index.html)。然后在需要引用模板文件的地方,采用模板文件相对地址硬编码时,形式如下:appname/index.html.

例如在views.py中

def index(request):
    #return HttpResponse("Hello, world. You're at the polls index.")
    return render(request,'polls/index.html')

或者index.html中

<li><ahref="/polls/xxx.html">{{question.question_text}}</a></li>



这根URL命名空间不一样:URL命名空间是用于模板文件中对URL(所谓URL是浏览器访问时的网络地址)的反向引用,一般会使用模板标签{% url %}

例如在index.html中

{% url 'polls:detail' question.id %}"



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现上传文件到指定路径,可以使用Django自带的FileField和Form组合来完成。具体步骤如下: 1.在forms.py中定义一个Form,用于处理上传的表单数据,例如: ```python from django import forms class UploadForm(forms.Form): file = forms.FileField() ``` 2.在views.py中定义一个视图函数,用于接收用户提交的表单数据,并将上传的文件保存到指定路径中,例如: ```python import os from django.conf import settings from django.shortcuts import render from .forms import UploadForm def upload(request): if request.method == 'POST': form = UploadForm(request.POST, request.FILES) if form.is_valid(): file = request.FILES['file'] path = os.path.join(settings.MEDIA_ROOT, file.name) with open(path, 'wb') as f: for chunk in file.chunks(): f.write(chunk) return render(request, 'upload_success.html') else: form = UploadForm() return render(request, 'upload.html', {'form': form}) ``` 这里使用了Python的open函数来打开一个文件并写入上传的文件内容,通过chunks方法将文件内容分块写入,以避免一次性读取大文件时占用过多内存。 3.在upload.html模板中定义一个表单,用于上传文件,并将其提交到视图函数中处理,例如: ```html <form method="post" enctype="multipart/form-data"> {% csrf_token %} {{ form.as_p }} <button type="submit">上传</button> </form> ``` 4.在settings.py中设置MEDIA_ROOT和MEDIA_URL,用于指定上传文件的存储路径和访问路径,例如: ```python MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/' ``` 5.最后,需要在项目的urls.py中添加一个URL模式,用于将上传文件的访问路径映射到MEDIA_ROOT下的实际文件路径,例如: ```python from django.conf.urls.static import static from django.conf import settings urlpatterns = [ # ... ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) ``` 这样就可以实现上传文件到指定路径了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值