通过Django发布json中的内容

时间:2019-05-01 04:16:37

标签: django

要构建一个简单的RESTful演示,如下所示: 选择歌曲(在html选项中显示歌曲ID /歌曲)以显示json中的歌曲信息

models.py

class Music(models.Model):
    song = models.CharField(max_length=30)
    singer = models.CharField(max_length=30)

music_pick.html

{% extends 'main/base.html' %}
{% block content %}
<select id="music_select">
  <option  value="" selected="selected">---Song Select---</option>
  {% for m in musics %}
   <option value="option{{ m.id }}" selected="selected">
       {{ m.song }}
   </option>
  {% endfor %}
</select>
<p>The song, {{ song }} was sang by {{ singer }}</p>

{% endblock %}

并且我已经完成了json产生部分的URL'music / <^ int:pk ^>' 它会返回:

{
    "id": 1,
    "song": "ICE AND FIRE",
    "singer": "George  Martin",
}

如何在Django 2中构建views.py或其他文件?

0 个答案:

没有答案