Reputation: 31
I am brand new to django.
This is the api json value I called from internet:
{'message': '',
'result': [{'name': 'ABC', <------
'Type': 'True', <-----
'Fee': 0.0005},
{'name': 'DEF', <-------
'Type': 'False', <------
'Fee': 0.004},
}
I want to put name and type to the webpage using HTML.
ABC True
DEF False
Do I need to modify models.py? Please give me an example. thank you The only think I know is to use for loop in HTML, but I do not know does it work?
Upvotes: 0
Views: 886
Reputation: 2966
Just using forloop in template.
{% for r in result %}
{{ r.name }} {{ r.Type }}
{% endfor %}
view
your result
is come from context
.
Basically, django view
render data to your own template. context_processors
do that for you, and it's defined in settings.py
(TEMPLATES
- OPTIONS
- context_processors
).
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
str(ROOT_DIR.path('templates')),
],
# 'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
# you can add your own processors
'your_app.path.to.custom_processors',
],
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
},
},
]
Here's more detail information about context
(official django docs)
It's why you can use request
, user
and other context data in your template.
If you want to use context in all templates, you can add your own context_processors
. But in many times, you just want context only in particular template. (like your situation).
Then you can add your data to context, in your views
.
If you're using Class Based View (I recommend using CBV), you can add by get_context_data()
. like this.
class ProductView(ListView):
model = Product
template_name = 'best.html'
context_object_name = 'products'
def get_context_data(self, **kwargs):
context = super(ProductView, self).get_context_data(**kwargs)
context['test'] = 'test context data'
return context
You can add your own context data in existing context
.
If you're using FBV, you can render template with your context. There're many ways to render (render
, render_to_response
, and so on.. You can check django docs for this. I guess you should use render
cause render_to_response
deprecated in django 2.0
)
from django.shortcuts import render
def my_view(request):
# View code here...
context = {'test': 'test coooontext data' }
return render(request, 'myapp/index.html', context)
By passing your own context data in views, you can use this in template.
So if you want to pass only 'name' and 'type', you can pass data just you want to use, not all. It's more effective working in view then template. I hope it helps.
Upvotes: 2
Reputation: 3371
In view.py
you can return data like
url = 'xxx'
response = requests.get(url)
# if your response is like : {'message': '', 'result': [{'name': 'ABC', 'Type': 'True', 'Fee': 0.0005}, {'name': 'DEF','Type': 'False', 'Fee': 0.004}] }
# then
list_result = response['result']
render(request, 'template.html', {"list_result": list_result})
Then you can send that list_result
on HTML page and import render
using
from django.shortcuts import render
To display data of response on HTML page use:
{% for data in list_result %}
{{ data.name }} {{ data.Type }}
{% endfor %}
Upvotes: 0