我有以下Django设置:
views.py
from .forms import Method1Form
def method1_input(request):
if request.method == 'POST':
method1_form = Method1Form(request.POST, request.FILES)
# Handle file upload
if method1_form.is_valid():
q = method1_form.save()
q.save()
# This is where we run code
# with the given parameters
# Do something q.run_code1()
# This will show the method1_result to be executed.
return HttpResponseRedirect(reverse('method1-result', kwargs={'id': q.id }))
else:
method1_form = Method1Form()
return render(request, 'my_app/method1.html', {'m1_form':method1_form})
urls.py
from django.conf.urls import include, url
from myapp import views
urlpatterns = [url(r'^method1/input$', views.method1_input, name='method1-input')]
现在,当前模板 my_app / method1.html 如下所示。请注意,它包含导航栏。
{% load static from staticfiles %}
<!DOCTYPE html>
<html>
<head>
<link rel="icon" type="image/png" href="{% static "images/favicon.png" %}"/>
<link rel="stylesheet" href="{% static "css/bootstrap.min.css" %}"/>
<script src="{% static "vendors/jquery-2.1.4.min.js" %}"></script>
<script src="{% static "vendors/bootstrap.min.js" %}"></script>
<script src="{% static "vendors/bootstrap.file-input.js" %}"></script>
<link rel="stylesheet" href="{% static "css/style.css" %}"/>
<meta charset="utf-8">
<title>Cool title</title>
</head>
<body>
<!--- BEGINNING OF NAVBAR -->
<nav class="navbar navbar-inverse" style="border-radius:0px;">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand active" href="{% url 'index' %}">ICEPOP</a>
</div>
<div>
<ul class="nav navbar-nav">
<li><a href="{% url 'analysis-method' %}">Home</a></li>
<li><a href="{% url 'about' %}">About</a></li>
<li><a href="{% url 'help' %}">Help</a></li>
<li><a href="{% url 'contact' %}">Contact</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="{% url 'apidoc' %}"><span class="glyphicon glyphicon-cutlery"></span> API</a></li>
</ul>
</div>
</div>
</nav>
<!--- END OF NAVBAR -->
<h1> Content specific to METHOD1 </h1>
</body>
</html>
我想要做的是将文件分成仅包含导航栏和base.html
的{{1}}模板。因此,在一天结束时,method1_only.html
文件将仅包含method1_only.html
的继承以及特定于其的文本。在Django中进行此类包装的方案是什么?
答案 0 :(得分:0)
嗯,django建议使用base.html文件不仅包含导航栏,而且包含所有页面的所有普通员工:网站上有块,导航栏,页脚,侧面板。所以我认为你应该采取下一步的方式:
base.html
{% load static from staticfiles %}
<!DOCTYPE html>
<html>
<head>
<link rel="icon" type="image/png" href="{% static "images/favicon.png" %}"/>
<link rel="stylesheet" href="{% static "css/bootstrap.min.css" %}"/>
<script src="{% static "vendors/jquery-2.1.4.min.js" %}"></script>
<script src="{% static "vendors/bootstrap.min.js" %}"></script>
<script src="{% static "vendors/bootstrap.file-input.js" %}"></script>
<link rel="stylesheet" href="{% static "css/style.css" %}"/>
<meta charset="utf-8">
<title>Cool title</title>
</head>
<body>
<!--- BEGINNING OF NAVBAR -->
<nav class="navbar navbar-inverse" style="border-radius:0px;">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand active" href="{% url 'index' %}">ICEPOP</a>
</div>
<div>
<ul class="nav navbar-nav">
<li><a href="{% url 'analysis-method' %}">Home</a></li>
<li><a href="{% url 'about' %}">About</a></li>
<li><a href="{% url 'help' %}">Help</a></li>
<li><a href="{% url 'contact' %}">Contact</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="{% url 'apidoc' %}"><span class="glyphicon glyphicon-cutlery"></span> API</a></li>
</ul>
</div>
</div>
</nav>
<!--- END OF NAVBAR -->
<h1>{% block method_content %}{% endblock method_content %}</h1>
</body>
</html>
在您的method1_only.html中,您将扩展base.html并将您的方法内容放在method_content块中,就像这样
{% extends "base.html" %}
{% block method_content %}Here goes your method content{% endblock method_content %}
希望这会有所帮助。