您好,我是Django的新用户,我尝试仅从视图中输入没有form.py的表单输入数据库,但是我获得名称错误,请帮助我简化问题
这是我的html表单代码
<form class="w3-container w3-card-4 w3-light-grey w3-text-black w3-margin" method="post" action="/test_submit/" id='myForm'>
{% csrf_token %}
<div class="w3-row-padding">
<div class="w3-half">
<p><label>Item Name</label></p>
<p><input class="w3-input" type="text" name="item_name" required></p>
</div>
<div class="w3-half">
<p><label>Brand Name</label></p>
<p><input class="w3-input" type="text" name="brand_name" required></p>
</div>
</div>
<div class="w3-row-padding">
<div class="w3-half">
<p><label>Item Size</label></p>
<p><select class="w3-select" name="item_size" required>
<option value="" disabled selected>Select Size</option>
<option value="5k-7k">5k-7k</option>
<option value="7k-10k">7k-10k</option>
<option value="11-13">11-13</option>
<option value="1-3">1-3</option>
<option value="4-5">4-5</option>
<option value="6-9">6-9</option>
<option value="10-11">10-11</option>
</select></p>
</div>
<div class="w3-half">
<p><label>Item Color</label></p>
<p><input class="w3-input" type="text" name="item_color" required></p>
</div>
<div class="w3-half">
<p><label>Item Unit</label></p>
<p><select class="w3-select" name="item_unit" required>
<option value="" disabled selected>Select Unit</option>
<option value="Number">Number</option>
<option value="Number">Number</option>
<option value="Boxes">Boxes</option>
</select></p>
</div>
<div class="w3-half">
<p><label>Quantity</label></p>
<p><input class="w3-input" type="number" name="item_quantity" placeholder="0" required></p>
</div>
</div>
<div class="w3-row-padding">
<div class="w3-half">
<p><label>Purchase Price</label></p>
<p><input class="w3-input" type="number" name="purchase_price" placeholder="0.0" required></p>
</div>
<div class="w3-half">
<p><label>Selling Price</label></p>
<p><input class="w3-input" type="number" name="selling_price" placeholder="0.0" required></p>
</div>
<div class="w3-half">
<p><label>MRP</label></p>
<p><input class="w3-input" type="number" name="mrp" placeholder="0.0" required></p>
</div>
<div class="w3-half">
<p><label>Date</label></p>
<p><input class="w3-input" type="date" name="date" required></p>
</div>
</div>
<div class="w3-bar">
<button class="w3-btn w3-blue w3-round">Save</button>
<button class="w3-btn w3-blue w3-round">Save & Generate Barcode</button>
<button class="w3-btn w3-gray w3-round" onclick="clearFun()">Cancle</button>
</div>
<p></p>
</form>
这是我的views.py代码
from django.shortcuts import render
from .models import Items_Tb
from django.contrib import messages
# Create your views here.
def items_views(request):
return render(request,'itemapp/items.html')
def items_submit_views(request):
print('----------------test my submit form-----------------')
itemname = request.POST[item_name]
brandname = request.POST[brand_name]
itemsize = request.POST[item_size]
itemcolor = request.POST[item_color]
itemunit = request.POST[item_unit]
itemquantity = request.POST[item_quantity]
purchaseprice = request.POST[purchase_price]
sellingprice = request.POST[selling_price]
mrp = request.POST[mrp]
item_date = request.POST[date]
item_info = Items_Tb(item_name=itemname,brand_name=brandname,item_size=itemsize,
item_color=itemcolor,item_unit=itemunit,item_quantity=itemquantity,purchase_price=purchaseprice,
selling_price=sellingprice,mrp=mrp,item_date=item_date)
item_info.save()
messages.success(request,'Items save successfully!')
return render(request,'itemapp/items.html')
这是models.py代码
from django.db import models
# Create your models here.
class Items_Tb(models.Model):
item_name = models.CharField(max_length=64)
brand_name = models.CharField(max_length=64)
item_size = models.CharField(max_length=64)
item_color = models.CharField(max_length=64)
item_unit = models.CharField(max_length=64)
item_quantity = models.IntegerField()
purchase_price = models.FloatField()
selling_price = models.FloatField()
mrp = models.FloatField()
item_date = models.DateField()
def __str__(self):#show tabel name on admin
return self.item_name
这是我的Urls.py代码
from django.contrib import admin
from django.urls import path
from dashboardapp import views as d_views
from itemapp import views as i_views
from userapp import views as u_views
urlpatterns = [
path('admin/', admin.site.urls),
path('dashboard/', d_views.dash_views),
path('test/', d_views.test_views),
path('items/', i_views.items_views, name='items'),
path('test_submit/', i_views.items_submit_views, name='test_submit'),
path('users/', u_views.users_views),
]
当我汇总表格时我收到此错误波纹管enter image description here
item_name
应该用引号引起来: