Reputation: 39
def order(request, item_id):
item = get_object_or_404(Item, pk=item_id)
if request.method == "POST":
username = None
username = request.user.username
userInfo = username + '/' + request.POST["nameNum"]
userAddr = request.POST["address"]
new_order = Review.objects.create(
reviewText = userInfo,
votes = userAddr
)
return HttpResponse("<script>alert('" + userInfo + '/ ' + userAddr + "');</script> 등록 완료")
return render(request, 'order.html', {'item': item})
This is my views.py, and
class Item(models.Model):
def __str___(self):
return self.item_name
item_name = models.CharField(max_length=100)
item_text = models.CharField(max_length=300)
pub_date = models.DateTimeField('date published')
item_amount = models.CharField(max_length=99999999)
item_image = models.ImageField(upload_to='image/', blank=True)
item_id = models.AutoField(primary_key=True)
this is my models.py.
And when I run this part, NOT NULL constraint failed: shop_review.item_id << this error occurs. How can I fix this error?
Upvotes: 1
Views: 523
Reputation: 477318
The review you are creating has no reference to its item, hence the error, you should specify the item id with:
new_order = Review.objects.create(
reviewText = userInfo,
votes = userAddr,
item_id=item_id
)
It might however be a good idea to make use of a Form
[Django-doc] to validate and clean the data. Right now there is no guarantee that the request.POST
will contain an address
for example.
Note: In case of a successful POST request, you should make a
redirect
[Django-doc] to implement the Post/Redirect/Get pattern [wiki]. This avoids that you make the same POST request when the user refreshes the browser.
Upvotes: 1