2016-08-04 57 views
-2

我正在用django製作一個web應用程序,並且在一部分中,我嘗試使用{%extends%}命令將一些HTML從一個模板到另一個。下面是代碼:爲什麼我的{%extends%}命令在我的django應用程序中工作

home.html的 -

<!doctype html> 
<html> 
<head> 
</head> 
<body> 
{% block content %} 
{% endblock %} 
</body> 
</html 

search.html -

{% extends "gamelobby/home.html" %} 
{% block content %} 
<h1>Hello World</h1> 
{% endblock %} 

任何想法,問題可能是什麼?

代碼home.html的觀點 -

def index(request): 
    all_games = GameCard.objects.all() 
    template = loader.get_template('gamelobby/home.html') 
    context = { 
     'all_games': all_games, 
    } 
    return HttpResponse(template.render(context, request)) 
+6

你還沒告訴我們什麼是行不通的?你有錯誤嗎? – solarissmoke

+2

我會猜測,並說你仍然在視圖中引用'home.html' ...請嘗試創建一個[mcve] – Sayse

+0

@solarissmoke不,我沒有得到一個錯誤,它只是沒有一個HTML search.html顯示 – dmkanerer

回答

1

要發生什麼是引導人們到你的搜索view使視圖有了解search.html

def index(request): 
    all_games = GameCard.objects.all() 
    template = loader.get_template('search.html') <!-- or whichever file --> 
    context = { 
     'all_games': all_games, 
    } 
    return HttpResponse(template.render(context, request)) 

當這種觀點的負載該模板,它從gamelobby/home.html看到這個extends並將其拉到左右塊標籤。

+0

是的,實際上工作,謝謝! – dmkanerer

相關問題