提问人:zahra shokrizadeh 提问时间:8/17/2023 最后编辑:zahra shokrizadeh 更新时间:8/17/2023 访问量:21
在 Django 中提交表单和调试
Submit the form and debugging in Django
问:
我想创建一个表单,以便用户可以注册产品。 但是,表单已创建,但未提交。 原因是什么?
并且在选择状态时,产品页面上记录的不是旧版本或新版本,而是 n 和 o。 为什么?
views.py:
# create list
@login_required(login_url="login")
def create_list(request):
if request.method == "POST":
item = List()
item.user = request.user.username
item.title = request.POST["create_list_title"]
item.category = request.POST["category"]
item.status = request.POST["status"]
item.description = request.POST["create_list_description"]
item.first_bid = request.POST["create_list_initial_bid"]
item.image_url = request.POST["img_url"]
item.save()
return redirect("index")
return render(request, "auctions/create_list.html")`
create_list.html:
{% extends "auctions/layout.html" %}
{% block body %}
<h2 id = "h2">Create List</h2>
<div>
<form method="POST">
{% csrf_token %}
<a id ="h4">Title : <input required placeholder="Title" autofocus type="text" name="create_title"/>
<a id = "h4">Category : <input required placeholder="Category" type="text" name="category"/></a>
<a id = "h4">Description : <textarea placeholder="Add Description" required type="text" name="create_desc"></textarea></a>
<a id = "h4">Starting bid : <input required placeholder="Starting bid" type="number" name="create_initial_bid"/></a>
<a id = "h4">Status : <input required placeholder="Status" type="text" name="status"/></a>
<form action="upload.php" method="post" enctype="multipart/form-data">
<label id="h4" for="fileUpload">Upload Image:</label>
<input id="h4" type="file" id="fileUpload" name="fileUpload">
</form><br>
<a id = "h4">Image_url :<input placeholder="Image URL" type="text" name="img_url"/></a><br>
<button id = "button" class="btn btn-outline-primary" type="submit">Submit</button>
</form>
</div>
{% endblock %}
models.py:
class List(models.Model):
choice = (
("n", "new vertion"),
("o", "old vertion")
)
user = models.CharField(max_length=64)
title = models.CharField(max_length=64)
description = models.TextField()
category = models.CharField(max_length=64)
first_bid = models.DecimalField(max_digits=10, decimal_places=2)
image = models.ImageField(upload_to="img/", null=True)
image_url = models.CharField(max_length=228, default = None, blank = True, null = True)
status = models.CharField(max_length=1, choices= choice)
active_bool = models.BooleanField(default = True)
答: 暂无答案
评论