我想弄清楚如何將原始字段數據及其結果發佈到我的結果頁面。我有一個用Django編寫的應用程序,它有兩個html頁面。輸入頁面和結果頁面。輸入頁面有一個表單。結果頁面有輸出。下面是我在的地方代碼:Django參考原始文章數據
models.py
class IssuingCA (models.Model):
ICA_name = models.CharField(max_length=200)
filepath = models.CharField(max_length=200)
def __str__(self):
return self.ICA_name
views.py(簡體版)
def index(request):
issuers = IssuingCA.objects.order_by('ICA_name')
issuerOptions = {'issuers': issuers}
return render(request, 'index.html', issuerOptions)
def results(request):
issuer = request.POST['selectedIssuer']
issuername = 'Where I want the ICA_name value "icas" displayed'
info = {'issuer': issuer, 'issuername': issuername}
return render(request, 'results.html', info)
的index.html
--snip--
<label for="selectedIssuers">Issuing CA</label>
<select id="selectedIssuers" name="selectedIssuers">
{% for icas in issuers %}
<option value="{{ icas.filepath }}">{{ icas }}</option>
{% endfor %}
--snip--
results.html
--snip--
<div>{{ issuer }}</div>
<div>{{ issuername }}</div>
--snip--
讓我們爲DB設置一些示例數據: ICA_Name =「squirrels」;文件路徑=「在/某些/森林」
應用程序的工作方式是,它提出了一個下降,顯示所有值的ICA_name
index.html頁面了。當您選擇ICA_name
(在這種情況下爲「松鼠」並提交時,它會傳遞松鼠的filepath
選項的值 - 「in/some/forest」,我通過POST請求並顯示在結果頁面上。我想要的是一種方法,也顯示在結果頁面上的松鼠,原來的價值,從而爲issuer
,在<div>
文本顯示松鼠(這個目前工程)和IssuerName
,在<div>
文本顯示在/ some/forest。
嗯..我從來沒有想過使用pk值和引用字段。我也不知道你可以寫{issuer':issued_ca.filepath}。你教過我一些新的東西!我剛剛測試過它,它的工作原理!謝謝!我已經查看了以前的表單,並且打算更新我的應用程序以在將來的版本中使用它們。 – Eddi
儘快完成,不要等待。即使不是用於安全功能,也可以節省大量時間,特別是在維護/擴展已經編寫的代碼時(它始終是主時鐘庫)。 –