[英]How can i add something to the database with a submit button in Django?
我正在 django 中制作一個雜貨清單 web 應用程序,我有一個包含您的雜貨清單的頁面,我有一個包含您可以添加到清單中的所有產品的頁面。
每個產品都有一個“添加到列表”按鈕。 目的是當您單擊該按鈕時,該產品會自動添加到雜貨列表中。 有人知道該怎么做嗎? 先感謝您。
模型.py
from django.db import models
# Create your models here.
class Brand(models.Model):
name = models.CharField(max_length=200, null=True)
def __str__(self):
return self.name
class AllProducts(models.Model):
name = models.CharField(max_length=200, null=True)
def __str__(self):
return self.name
class ShoppingList(models.Model):
product = models.ForeignKey(AllProducts, null=True, on_delete= models.SET_NULL, blank=True)
brand = models.ForeignKey(Brand, null=True, on_delete= models.SET_NULL, blank=True)
quantity = models.CharField(max_length=200, null=True, blank=True)
info = models.TextField(max_length=500, null=True, blank=True)
def __str__(self):
return self.product
class 品牌是 class 與所有品牌的產品。 class All_Products 是 class,其中包含您可以添加到雜貨列表中的所有產品。 class ShoppingList 是一個 class,其中包含雜貨清單中的所有產品。
視圖.py
def home(request):
products = ShoppingList.objects.all()
context = {
'products':products,
}
return render(request, 'groceries_list/home.html', context )
def all_products(request):
all_products = AllProducts.objects.all()
context = {
'products':all_products,
}
return render(request, 'groceries_list/all_products.html', context)
The home function is the function that handels the groceries list page an the all_products function is the function that handels the page with all the product you can add to your list.
雜貨清單模板
{% extends "groceries_list/base.html" %}
{% block content %}
<div class="card">
<div class="card-body">
<div class="card m-1 text-white">
<a href="{% url 'create_grocery' %}" class="btn btn-success btn-md btn-block ">Add Grocery</a>
</div>
{% for product in products %}
<div class="item-row">
<div class="card m-1 text-white" style="background-color: #9BD6E0;">
<div class="card-body">
<a class="btn btn-sm btn-info" href="{% url 'update_gorcery' product.id %}">Update</a>
<a class="btn btn-sm btn-danger" href="{% url 'delete_gorcery' product.id %}">Delete</a>
<span class="text-dark"><strong>{{product.product}}</strong> {{product.quantity}} </span>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
所有產品模板
% extends "groceries_list/base.html" %}
{% block content %}
<div class="card">
<div class="card-body">
<div class="card m-1 text-white">
<a href="{% url 'create_product' %}" class="btn btn-success btn-md btn-block ">Add Product</a>
</div>
{% for product in products %}
<div class="item-row">
<div class="card m-1 text-white" style="background-color: #9BD6E0;">
<div class="card-body">
<button type="submit" class="btn btn-success btn-sm ">Add To List</button>
<span class="text-dark ml-3 text-center"><strong>{{product.name}}</strong>
<a class="btn btn-sm btn-danger float-right" href="{% url 'delete_product' product.id %}">Delete</a>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
該站點可能有許多users
同時使用它,因此您應該安排一種方法來識別將商品添加到購物清單的特定user
。 最簡單的方法是從django.auth.models
創建一個身份驗證系統(您可以按原樣使用或擴展User
模型)查看您的視圖,它們是home()
和all_products()
視圖正在呈現相同的上下文,這絕對不是購物車中的內容(對於主視圖)。
處理此問題的一種方法是使您的ShoppingList
model 包含一個customer
字段。 客戶將是提出request
的用戶。 在all_products.html
頁面中,您可以創建一個帶有隱藏字段的表單,您可以使用product.id
和“添加到列表”按鈕作為此表單的提交按鈕預先填充該隱藏字段。 當用戶單擊“添加到列表”時,表單將發布到調用負責視圖的 url。 在該視圖中,您使用product.id
ShoppingList object
和客戶作為ShoppingList
(發出request.user
的用戶)。 只是一些隨機提示:在您的ShoppingList model
中,您將數量定義為CharField
但數量最好定義為IntegerField
。 此外,不需要同時使用blank=True
和null=True
arguments。 我個人喜歡使用blank=True
只是出於安全原因,我不會在這里討論。 我還建議您修改模型和視圖的命名系統。
總之:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.