1
在我的項目中,我有2個模型文章和報價。每次用戶點擊主頁上的一個按鈕,它都會將相關的報價添加到用戶的文章中。如何在不重新加載的情況下將其返回主頁
的Article.models是這樣的:
class Article(models.Model):
user = models.OneToOneField(User, null=True)
quote = models.ManyToManyField(Quote, blank=True, null=True)
def __unicode__(self):
return self.user.username
這裏是view.py
def add_quote(request, id):
u = User.objects.get(username=request.user)
a = Article.objects.get(user=u)
q = Quote.objects.get(id=id)
a.quote.add(q)
a.save()
return HttpResponseRedirect(reverse("home"))
的Home.html:
{% for quote in quotes %}
<p>{{ quote.description }}</p>
<p><a class="btn btn-primary" href="{{ quote.get_absolute_url }}"
role="button" data-container="body" data-toggle="popover"
data-placement="top" data-content="added">collect »</a></p>
它的工作。但是,它也會重新加載主頁。所以當我向下滾動並點擊按鈕時,頁面會回到頂部,並且不會保留在我點擊的位置。
我做了一些研究發現,dajax可能會幫助,但不知道如何解決我的問題或其他有效的方式嗎?
非常感謝Sam。第一種方法工作得很好。請問你能告訴我更多關於第二種方法的信息嗎? – Leonsion 2015-02-06 09:09:21