简体   繁体   English

为什么 Django 中的 POST 请求有时会起作用,而其他时候则不起作用? (几乎是随机的)

[英]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.

相关问题 为什么Django的缓存可以与locmem一起工作但是memcached失败了? - Why would Django's cache work with locmem but fail with memcached? 相同的Django单元测试有时会失败,有时会成功 - Same Django unit test fails sometimes and succeeds at other times 为什么通过 Python `requests` 发送请求的时间几乎是 Postman 的 7 倍? - Why does a request via Python `requests` takes almost seven times longer than in Postman? 为什么 DF 有时会自动更新,有时不会? - Why does a DF sometimes automatically update, other times does NOT? 为什么在 255 倍时图片会变成紫色? - Why would picture become purple when it's times 255? 从Twitter-api返回数据时,为什么有时会出现python“ KeyError:”错误,而在其他时候则不会出现? - Why is there a python “KeyError:”error appearing sometimes and then not other times when returning data from Twitter-api? 为什么我必须运行 pip 安装<package name>有时有时不是?</package> - Why do I have to run pip install <package name> sometimes and other times no? 为什么它的工作方式不同,即使它在 python 尝试中几乎相同的代码除外? - Why does it work differently even though it's almost the same code in python try except? output 的最小值/最大值有时正确,有时不正确 - output for min/max sometimes correct, other times not 身份验证会话中的请求有时由Django中的AnonymousUser发送 - Requests in authenticated session are sometimes sent by AnonymousUser in Django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM