[英]Why would POST requests in Django work sometimes and not the other times? (It's almost random)
I have a Django CRUD web app that is basically used to add values to the database, which are visible on the frontend when the page is refreshed and there is a button to edit that entry.我有一个 Django CRUD web 应用程序,它基本上用于向数据库添加值,当页面刷新时这些值在前端可见,并且有一个按钮可以编辑该条目。 It works perfectly fine 90% of the time.
它在 90% 的时间里都能正常工作。 But there will be a random instance where someone adds something using the UI and it is added to the database but when refreshed, the entry disappears from the database.
但是会有一个随机实例,其中有人使用 UI 添加一些内容并将其添加到数据库中,但是当刷新时,该条目会从数据库中消失。 It's the most absurd thing I've ever seen.
这是我见过的最荒谬的事情。 If something was wrong with the code wouldn't it never add it correctly.
如果代码有问题,它不会永远不会正确添加它。 I'm just confused why it adds values fine 90% of the time but there's that few instances of disappearing entries.
我只是很困惑为什么它在 90% 的情况下都可以很好地添加值,但是很少有条目消失的情况。
def index(request, user, created_by):
if request.POST:
print(request.POST)
selected_user = request.POST.get('name_user')
selected_project = request.POST.get('name_project')
print(selected_user)
week_starting = request.POST.get('date')
hours = request.POST.get('hours')
activity = request.POST.get('activity')
selected_tag = request.POST.get('tag')
# check if record already exists for the same user and same week_starting and selected_project
# if not then insert to DB
# else don't insert, say record already exists
if selected_tag == '':
check_if_exists = TABLENAME.objects.filter(username=selected_user,
week_starting=week_starting,
project_id=selected_project, tag_id__isnull=True)
else:
check_if_exists = TABLENAME.objects.filter(username=selected_user,
week_starting=week_starting,
project_id=selected_project,
tag_id=selected_tag)
count = check_if_exists.count()
print("at insert")
print("how many same records already exist: ", count)
if count < 1:
selected_project_id = TABLENAME.objects.only('project_id').get(
project_id=request.POST.get('project'))
print(selected_project)
if selected_tag == '':
TABLENAME.objects.create(username=selected_user, project_id=selected_project_id,
week_starting=week_starting, forecast_hours=hours,
description=activity,
created_on=timezone.now(), created_by=created_by)
add_status = "added - no tag"
else:
TABLENAME.objects.create(toggl_username=selected_user,
project_id=selected_project_id,
week_starting=week_starting, forecast_hours=hours,
description=activity, tag_id=selected_tag,
created_on=timezone.now(), created_by=created_by)
add_status = "added - with tag"
flash_text = 'Record inserted'
print(add_status)
# selected_tag = 0
return render(request, 'hello_v2.html',
{'flash_text': flash_text, 'users': users, 'selected_user': selected_user,
'projects': projects, 'date': date, 'tags': tags,
'selected_tag': selected_tag, 'selected_project': selected_project})
# add if tag is not empty then pass tags like here else don't - do the same thing for ID
else:
flash_text = 'Record already exists'
return render(request, 'hello_v2.html',
{'flash_text': flash_text, 'users': users, 'selected_user': selected_user,
'projects': projects, 'date': date, 'tags': tags, 'selected_tag': selected_tag,
'selected_project': selected_project})
else:
print("Loading page")
print(date)
selected_tag = request.GET.get('tag_id')
return render(request, 'hello_v2.html', {'users': users, 'date': date, 'projects': projects, 'tags': tags, 'selected_tag': selected_tag})
I'm tracking the POST requests by printing them and in the instances where the entry disappears from the database, I can't see the POST request printed... So I'm really confused I also have an edit method which enables users to edit an entry, sometimes when users edit an entry then it disappears so maybe I have an issue there?我正在通过打印它们来跟踪 POST 请求,并且在条目从数据库中消失的情况下,我看不到打印的 POST 请求......所以我真的很困惑我还有一个编辑方法可以让用户编辑一个条目,有时当用户编辑一个条目时它会消失,所以也许我有问题? But it's only some edits that cause that, not all.
但只有一些编辑会导致这种情况,而不是全部。
You should check the request like this.您应该像这样检查请求。
def index(request, user, created_by):
if request
.method == "POST":
print(request.POST)
selected_user = reques
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.