【发布时间】:2017-05-23 03:54:29
【问题描述】:
我正在尝试创建一个添加到收藏夹功能,到目前为止,我设法显示一个带有图标的按钮,该按钮基于 is_favorite 字段,但我无法更新我的数据库。
我的问题是:如何在不刷新页面的情况下更改收藏状态并将其更新到我的数据库中
编辑: [已解决] 感谢 @Nazkter 和 Bucky Robert 的项目。我编辑了下面的工作代码
这是我的代码:
模型.py
class ProjectProfile(models.Model):
project_name = models.CharField(max_length=100)
artist = models.CharField(max_length=100)
is_favorite = models.BooleanField(default=False)
def __str__(self):
return self.project_name
views.py
def index_view(request):
context = {
"table_list": ProjectProfile.objects.all(),
"title": "Table_List"
}
return render(request, 'index.html', context)
def favorite_project(request, projectprofile_id):
projectprofile = get_object_or_404(ProjectProfile, pk=projectprofile_id)
try:
if projectprofile.is_favorite:
projectprofile.is_favorite = False
else:
projectprofile.is_favorite = True
projectprofile.save()
except (KeyError, ProjectProfile.DoesNotExist):
return JsonResponse({'success': False})
else:
return JsonResponse({'success': True})
url.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', index_view, name='index_view'),
url(r'^(?P<projectprofile_id>[0-9]+)/favorite_album/$', favorite_project, name='favorite_project'),
]
index.html
<head>
<meta charset="UTF-8">
<title>Favorite function</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link href='https://fonts.googleapis.com/css?family=Satisfy' rel='stylesheet' type='text/css'>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<h1>Favorite Function</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Artist</th>
<th>Favorite</th>
</tr>
</thead>
<tbody>
{% for data in table_list %}{% csrf_token %}
<tr >
<td>{{data.project_name}}</td>
<td>{{data.artist}}</td>
<!-- Favorite Album -->
<td><a href="{% url 'favorite_project' data.id %}" class="btn btn-default btn-sm btn-favorite" role="button"><span class=" glyphicon glyphicon-star {% if data.is_favorite %}active{% endif %}" action=""></span></a></td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
javascript
var ProjectListPage = {
init: function() {
this.$container = $('.project-container');
this.render();
this.bindEvents();
},
render: function() {
},
bindEvents: function() {
$('.btn-favorite', this.$container).on('click', function(e) {
console.log('Hola');
e.preventDefault();
var self = $(this);
var url = $(this).attr('href');
$.getJSON(url, function(result) {
if (result.success) {
$('.glyphicon-star', self).toggleClass('active');
}
});
return false;
});
}
};
$(document).ready(function() {
ProjectListPage.init();
});
【问题讨论】:
-
您是否有一个 python API 以允许 ajax 与之通信的方式公开您的数据库?如果是这样,您可以将单击事件绑定到按钮,并编写 jquery 以将 JSON 对象发布到您的 python API 端点,然后写入数据库。
-
你好@JacobIRR 谢谢你的意见,你指的是REST API对吗?我会尝试在我的代码中实现它。谢谢。
-
@Nazkter 已经领先于我了。这个答案就是我所建议的实现。
-
@JacobIRR 尽管如此,您的输入仍然非常感谢,谢谢
-
我已经尝试过了,它给了我一个错误(没有返回 HttpResponse 对象。它返回了 None。)
标签: javascript jquery python django