我一直在努力整理“添加到监视列表”,下面的方法说明了我如何在代码中不使用 Jquery 并保持简单的 Django/Python 的情况下实现这一目标。
型号:
class Startup ( models.Model ) :
author = models.OneToOneField ( User , on_delete = models.CASCADE )
startup_name = models.CharField ( max_length = 32 , null = False , blank = False )
wl = models.ManyToManyField(User, related_name = 'watchlist', blank = True)
def __str__(self) :
return str ( self.startup_name )
查看Global_list(列表)和监视列表功能:
@login_required
@inv_required
def global_list(request):
startup = Startup.objects.all()
return render(request, 'inv_template/global_list.html', {'startup': startup})
@login_required
@inv_required
def watchlist (request, id):
check = Startup.objects.get(id=id)
if check.wl.exists():
check.wl.remove(request.user)
check.save()
else:
check.wl.add(request.user)
check.save()
return redirect ( 'globallist' )
包含监视列表链接的全局列表:
{% extends 'inv_template/inv_base.html' %}
{% block content %}
{% include 'inv_template/inv_nav.html' %}
<div class="row" style="margin-top: 10px; font-size: 14px;background-color: rgba(255,255,255,0);">
{% for s in startup.all %}
<div class="col-lg-12 col-xl-12">
<div class="card" style="margin-bottom: 10px;">
<div class="card-body shadow">
<div class="row">
<div class="col-lg-12 text-center d-lg-flex justify-content-lg-end align-items-lg-center"><a href="{% url 'watchlist' s.id %}">
<i class="far fa-eye"></i><i class="fas fa-plus" style="font-size: 7px;"></i>
</a></div>
<div class="col-lg-3 text-center d-lg-flex justify-content-lg-center align-items-lg-center"><a href="{% url 'applicationdetail' s.id %}" type="submit" style="font-size: 29px;">{{s.startup_name}}</a></div>
<div class="col-lg-9">
<div class="row">
{% for g in s.generalinformation_set.all %}
<div class="col-lg-3"><label class="col-form-label" style="color: rgba(0,27,252,0.85);font-size: 12px;"><strong>Established</strong>:</label><br/>{{g.established}}</div>
<div class="col-lg-3"><label class="col-form-label" style="color: rgba(0,27,252,0.85);font-size: 12px;"><strong>Stage:</strong></label><br/>{{g.stage}}</div>
<div class="col-lg-3"><label class="col-form-label" style="color: rgba(0,27,252,0.85);font-size: 12px;"><strong>Type:</strong></label><br/>{{g.type}}</div>
<div class="col-lg-3"><label class="col-form-label" style="color: rgba(0,27,252,0.85);font-size: 12px;"><strong>Location:</strong></label><br/>{{g.location.name}}</div>
<div class="col-lg-12"><label class="col-form-label" style="color: rgba(0,27,252,0.85);font-size: 12px;"><strong>Business Stage:</strong></label> {{g.stage_select}}</div>
{% endfor %}
</div>
</div>
<div class="col-lg-3 d-lg-flex justify-content-lg-center align-items-lg-center"><label class="col-form-label" style="padding-left: 0px;font-size: 8px;">(<strong>0 </strong>Views)</label></div>
{% for f in s.fund_set.all %}
<div class="col"><label class="col-form-label" style="color: rgba(0,27,252,0.85);font-size: 12px;"><strong>Requested Fund:</strong></label> {{f.req_amount}}</div>
{% endfor %}
</div>
</div>
</div>
</div>
{% endfor %}
</div>
<div style="">
</div>
{% endblock %}
网址:
# Global List Router
path ( 'globallist/' , views.global_list , name = 'globallist' ) ,
# Watchlist Router
path ( 'watchlist/<int:pk>' , views.watchlist , name = 'watchlist' ) ,
最佳答案
以上代码是更新后的代码,说明了不使用Jquery添加到监视列表的方法。
关于Django 添加到监视列表 没有 Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62432263/