我一直在試圖開發一個django應用程序來處理欄標籤...到目前爲止,我可以添加標籤,將產品添加到選項卡,並從選項卡中刪除產品。現在,我想使用AJAX添加/刪除產品從一個選項卡,並且我不知道是否需要使用表格或不...我到目前爲止:技術到AJAX - 如果這個Django應用程序
模型。 PY
class Product(models.Model):
name = models.CharField(max_length='70')
price = models.FloatField()
def __unicode__(self):
return self.name
class Tab(models.Model):
number = models.IntegerField()
name = models.CharField(max_length='50')
tabdate = models.DateTimeField('date created')
consumed = models.ManyToManyField(Product, through='ConsumedRelation')
def __unicode__(self):
return self.name
class ConsumedRelation(models.Model):
tab = models.ForeignKey(Tab)
product = models.ForeignKey(Product)
count = models.PositiveIntegerField(blank=True, null=True, default=1)
def __unicode__(self):
return str(self.product)
views.py
def addproduct(request, number, product):
tab = Tab.objects.get(number=number)
product = Product.objects.get(id=product)
add = ConsumedRelation.objects.create(product=product, tab=tab, count=1)
add.save()
context = {'tab': tab, 'product': product}
return render_to_response('addproduct.html', context)
def deleteproduct(request, number, consumedid):
tab = Tab.objects.get(number=number)
ConsumedRelation.objects.filter(id=consumedid).delete()
context = {'tab': tab}
return render_to_response('deleteproduct.html', context)
urls.py
url(r'^tabs/(?P<number>[0-9].*)/delete/(?P<consumedid>[0-9].*)/$', 'barcomandas.views.deleteproduct'),
url(r'^tabs/(?P<number>[0-9].*)/add/(?P<product>[0-9].*)/$', 'barcomandas.views.addproduct'),
singletab.html
<h1>{{ tab.name }} | {{ tab.number }}</h1>
<h2>Consumed</h2>
{% for consumed in consumedlist %}
<a href="delete/{{ consumed.id }}">X</a>{{ consumed }}<br/>
{% endfor %}
<div id="addproducts">
{% for product in productlist %}
<li><a href="add/{{ product.id }}">{{ product.name }}</a></li>
{% endfor %}
</div>
addproduct.html
{{ product }} added to tab {{ tab.name }}
因此,例如,當我添加了一些產品,我有一個無用的頁面 「的產品添加到選項卡」因爲我不能沒有一些HTML頁面,所以我可以使用視圖。那有意義嗎? 但我不想把它作爲一個表單,因爲這將用於7「平板電腦,我會爲每個產品有很好的大小的按鈕,因爲我工作的酒吧有時候會變得很忙,所以我需要那樣的速度
理想情況下,我會在所有事情上都有ajax,左邊是當前打開的選項卡列表,右邊的列選項卡編輯選項可以添加產品,關閉選項卡,等等 所以我想要的是,在singletab.html中有產品列表,在每個產品的側面都有一個「X」。您點擊X,產品消失,列表和其他所有東西更新,無需重新加載頁面。
你們看到任何解決方案嗎?對不起我的en英語,不是我的主要語言。
PS: 這也是我的第一個Django應用程序,所以我很抱歉任何愚蠢的錯誤.. 會一個簡單的jQuery得到例如(/ tabnumber /添加/產品)就夠了嗎?我將如何實現它?