所有,django templatetag可以直接调用视图函数吗?我想将“ include html”标签与其他应用程序的表单数据一起使用。
我的项目结构如下:
a[(a[:,0] * (a[:,2] < 5).astype(int)).argmax(),:]
Out[946]: array([5, 1, 2])
我要在app_1.html中使用“ {%include app2.html%}”来访问属于app_2的app_2.html内容,并且其中包含的app_2.html表单数据属于app_2 / views(forms)。< / p>
同时,将在app_1.html中显示的“包含” app_2.html具有“提交” POST操作,我也需要检索发布的数据。 app_2 /文件夹中的代码如下:
app_2 / urls.py:
ProjectRoot
├── manage.py
├── MyProject
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── templates
│ ├── 404.html
│ ├── contact-us.html
│ ├── index.html
│ ├── tags_form.html
│ ├── tags_index.html
├── app_1
│ ├── admin.py
│ ├── apps.py
│ ├── forms.py
│ ├── __init__.py
│ ├── models.py
│ ├── templatetags
│ │ ├── __init__.py.bak
│ │ └── app1_tags.py.bak
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── app_2
│ ├── admin.py
│ ├── apps.py
│ ├── forms.py
│ ├── __init__.py
│ ├── models.py
│ ├── templatetags
│ │ ├── __init__.py
│ │ └── app2_tags.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
app_2 / views.py:
from django.conf.urls import url, include
from django.contrib import admin
from . import views
app_name = 'Test_Example'
urlpatterns = [
url(r'^logexplorertest/', views.index, name='index'),
]
app_2 / forms.py:
from django.http import HttpResponse
from django.shortcuts import render
from .forms import MyForm
from django.views.decorators.csrf import csrf_exempt
import subprocess
basePath="/var/log/"
@csrf_exempt
def index(request):
if request.method == 'POST':
form = MyForm(request.POST)
if form.is_valid():
getPostList=request.POST.getlist('field')
tailedLines=[]
listCmd = subprocess.Popen(["tail -n 10 %s%s"%(basePath,getPostList[0])], shell=True, stdout=subprocess.PIPE).stdout
fileTailLastLines = listCmd.read().splitlines()
for line in fileTailLastLines:
tailedLines.append('\n%s'%str(line, encoding = "utf-8"))
print ("Log file content has been retrieved and printed like on the screen." )
return render(request, 'logexplorer_index.html', {'form': form,'tailedLines':tailedLines})
else:
form = MyForm()
return render(request, 'logexplorer_index.html', {'form':form})
任何帮助将不胜感激。预先感谢。
答案 0 :(得分:0)
通过添加以下内容解决: 上下文['request']