【问题标题】:Database entry not created by clicking on the accept friend request button单击接受好友请求按钮未创建数据库条目
【发布时间】:2021-04-20 12:58:31
【问题描述】:

我正在写接受好友请求的视图,我不知道发生了什么, 我正在将 jason 数据提取到视图中,但我不工作。

我已经尝试了这个网站上的所有解决方案,但它们对我不起作用

这是我的 Javascript

<script >
function acceptFriendRequest(friend_request_id, uiUpdateFunction){
    var url = "{% url 'friend:friend-request-accept' friend_request_id=53252623623632623 %}".replace("53252623623632623", friend_request_id)
    $.ajax({
        type: 'GET',
        dataType: "json",
        url: url,
        timeout: 5000,
        success: function(data) {
            console.log("SUCCESS", data)
            if(data['response'] == "Friend request accepted."){
                // ui is updated
            }
            else if(data['response'] != null){
                alert(data['response'])
            }
        },
        error: function(data) {
            console.error("ERROR... this is the error from accept", data)
            alert("Something went wrong")
        },
        complete: function(data){
            uiUpdateFunction()
        }
    });
}

这是我的观点

def accept_friend_request(request, *args, **kwargs):
user = request.user
payload = {}
if request.method == "GET" and user.is_authenticated:
    friend_request_id = kwargs.get("friend_request_id")
    # print(f"friend request id {friend_request_id}")
    if friend_request_id:
        friend_request = FriendRequest.objects.get(pk=friend_request_id)
        # print(f"friend request object {friend_request}")
        # confirm that is the correct request
        if friend_request.receiver == user:
            if friend_request: 
                # found the request. Now accept it
                friend_request.accept()
                payload['response'] = "Friend request accepted."

            else:
                payload['response'] = "Something went wrong."
        else:
            payload['response'] = "That is not your request to accept."
    else:
        payload['response'] = "Unable to accept that friend request."
else:
    # should never happen
    payload['response'] = "You must be authenticated to accept a friend request."
return HttpResponse(json.dumps(payload), content_type="application/json")

这里是 accept()

def accept(self):
    """
    Accept a friend request
    update both SENDER and RECEIVER friend lists
    """
    receiver_friend_list = FriendList.objects.get(username = self.receiver)
    if receiver_friend_list:
        receiver_friend_list.add_friend(self.sender)
        sender_friend_list = FriendList.objects.get(user = self.sender)
        if sender_friend_list:
            sender_friend_list.add_friend(self.receiver)
            self.is_active = False
            self.save()

【问题讨论】:

  • accept() 函数的来源是什么?
  • 缺少很多东西,首先,您不应该从 kwargs 中获取查询参数,您应该从 request.GET.get('friend_request_id") 中获取。其次,您正在制作一个使用朋友请求 id 搜索 pk 进行查询,您需要了解“pk”指向数据库中的表行“id”,因此除非您自定义您的查询不会找到任何东西。您无法找到查询错误,您还需要粘贴错误,因为我们无法知道 python 报告的问题
  • GET 127.0.0.1:8000/friend/friend_request_accept/5/.py 404 (Not Found) send @ jquery-latest.js:9631 ajax @ jquery-latest.js:9176 acceptFriendRequest @ (index):339 triggerAcceptFriendRequest @ (index):326 onclick @ (index):234 (index):354 错误...这是从接受 {readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, ...}
  • @shivam,您收到 404 错误(意味着您尝试访问的页面不存在),如您所见,我认为您的 URL 末尾有一个 .py您要查找的是指向 127.0.0.1:8000/friend/friend_request_accept/5/ (请注意,URL 的末尾现在没有“.py”),这是冰山一角, django 甚至没有执行你的请求代码
  • 我已经从 url 中删除了 .py,仍然没有任何变化,但现在控制台上没有显示错误

标签: javascript python jquery django


【解决方案1】:

我经常遇到这样的问题,即在 Jquery Ajax / json 返回时看不到错误消息(使用 PHP)。将 dataType:"json" 更改为 "text",您可能会看到错误消息

【讨论】:

    猜你喜欢
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多