嘿伙计们,我的文件组织如下。
urls.py:
data: data,
views.py:
data: postData,//this contains the form data
basic.html
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^contact/', views.eurusd, name='eur'),
url(r'^contact/', views.VaR, name='lol'),
]
我现在的问题是: 为什么只有来自eur字典的字符串在我的名为basic.html的模板中返回而不是lol? 如何将多个变量传递给我的basic.html?
答案 0 :(得分:0)
您只需要一个网址格式并在视图中使用一个功能,将代码更改为:
urls.py:
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^contact/', views.eurusd, name='eur'),
]
views.py:
from django.shortcuts import render
from django.http import HttpResponse
from yahoo_finance import Currency
def eurusd(request):
eur_usd = Currency('EURUSD')
eur = ('EUR/USD Bid/Ask Price: ' +eur_usd.get_bid() +' / '+eur_usd.get_ask())
hallo = "this is a python sting"
return render(request, 'personal/basic.html', {'eur': eur,'lol': hallo})
答案 1 :(得分:0)
你不需要两个函数,因为你想用两个变量渲染模板,所以最简单的方法就是编写一个在上下文中返回两个wariables的函数。有人喜欢这样:
urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^contact/', views.eurusd, name='eur'),
]
views.py
eur_usd = Currency('EURUSD')
eur = ('EUR/USD Bid/Ask Price: ' +eur_usd.get_bid() +' / '+eur_usd.get_ask())
hallo = 'This is the python string'
return render(request, 'personal/basic.html', {'eur': eur, 'hallo' : hallo})
使用此代码,您的模板将正常工作
在你的情况下django在尝试查找url时,找到看起来像你的url的第一个url,然后他们确实要求第一个函数。