簡體   English   中英

如何使用 Django 中的提交按鈕向數據庫添加內容?

[英]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=Truenull=True arguments。 我個人喜歡使用blank=True只是出於安全原因,我不會在這里討論。 我還建議您修改模型和視圖的命名系統。

總之:

  • 將字段 customer 添加到 ShoppingList model。
  • 將現場產品設為 CharField。
  • 制作一個帶有隱藏字段的表單,當用戶單擊“添加到列表”時,該字段會回發 product.id。
  • 通過制作 ShoppingList object 來處理發布請求。
  • 考慮將數量設為 IntegerField。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM