【问题标题】:Django is not calling functionsDjango没有调用函数
【发布时间】:2018-10-08 08:48:28
【问题描述】:

我的 django 主站点网址

from django.contrib import admin
from django.urls import path,include

urlpatterns = [

    path('ways/', include('ways.urls')),
    path('admin/', admin.site.urls),

应用网址

urlpatterns = [

    path('newWay/', views.newWay, name='newWay'),
    path('waySolved/', views.update_way, name='update_way'),


]

项目结构

但是每当我点击http://localhost:8000/ways/waySolved/ 它说未找到:/ways/waySolved/http://localhost:8000/ways/newWay/ 完美运行。 在视图中我有这两种功能。 更新方式

def update_way(request):
    way_id= request.GET.get('way_id', None)
    token=request.GET.get('token', None)

    if way_id is not None and token is not None:
        token = token.replace("\"", "")

        community_osm_cur.execute("""SELECT  latitude ,longitude from (SELECT * from (SELECT  
            latitude,longitude,id from  current_nodes where id in (Select node_id from 
            current_way_nodes where way_id='%s' ORDER BY  sequence_id asc )) AS temp1
            Inner join
            (Select node_id,sequence_id from current_way_nodes where way_id='%s' 
            ORDER BY  sequence_id asc ) As temp2 on temp1.id=temp2.node_id
            order by sequence_id asc) As temp3""", ((int)(way_id), (int)(way_id),))
        nodes_list = community_osm_cur.fetchall()

        coords = []
        if len(nodes_list)>0:

            for node in nodes_list:
                lat = node[0] / 10000000
                lng = node[1] / 10000000
                coords.append([lat, lng])

            position_hash = hashlib.sha3_256(json.dumps(coords).encode()).hexdigest()
            community_osm_cur.execute("select user_id from oauth_tokens where secret = %s", (token,))
            id = community_osm_cur.fetchone()[0]



            updated_way_cur.execute("""INSERT INTO updated_ways (way_id,node_hash) values 
              (%s,%s) on conflict(way_id)  do update set node_hash=%s""",
              (way_id,position_hash,position_hash,))
            updated_way_cur.execute("Delete from distribution where user_id=%s and  id = %s ",(id,way_id,))
            updated_way_con.commit()
            return HttpResponse(status=200)
    return HttpResponse(status=404)

【问题讨论】:

  • 抱歉 newway 有一个斜线。它只是打印Not Found: /ways/waySolved/ "GET /ways/waySolved/?way_id=13232323 HTTP/1.1" 404 0 仅此而已
  • 显示您的视图函数
  • @Alasdair 视图已添加

标签: python django url url-pattern


【解决方案1】:

在您看来,这似乎是一个问题,而不是您的 URL 模式。

您正在访问/ways/waySolved/?way_id=13232323,前两行是。

def update_way(request):
    way_id= request.GET.get('way_id', None)
    token=request.GET.get('token', None)

查询字符串中没有token,因此您将拥有way_id=13232323token=None

然后你的代码会:

if way_id is not None and token is not None:
    ...
return HttpResponse(status=404)

由于token 为None,因此视图跳转到最后,因此预期会出现404 响应。

【讨论】:

  • 但是服务器正在打印** Not Found: /ways/waySolved/** 所以它可能还没有到达那里
  • 我已经用虚拟令牌对其进行了测试,但响应相同 ** 未找到:/ways/waySolved/ "GET /ways/waySolved/?way_id=13232323&token=646464 HTTP/1.1" 404 0* *
  • 您没有显示完整的错误消息,所以我无法确定,但它看起来好像是 update_way 视图中的问题,而不是您的 URL 模式。您需要向update_way 添加一些打印/日志记录,以查看正在执行的代码。即使token 不是None,也有其他以return HttpResponse(status=404) 结尾的代码路径。
  • 实际上它并没有给出任何错误,它只是打印以及代码打印的所有内容
  • 如果 404 是您的 URL 模式的问题,那么当您使用 DEBUG=True 运行时,您会在浏览器中看到黄色的调试页面。如果 404 来自 return HttpResponse(status=404) 行,那么您将不会得到那个 404 页面,我错了。我的建议和以前一样——在你的视图中添加打印/日志以找出它为什么会到达HttpResponse(status=404)
猜你喜欢
  • 1970-01-01
  • 2020-04-28
  • 2022-11-10
  • 2020-10-15
  • 1970-01-01
  • 2018-04-25
  • 1970-01-01
  • 2018-05-30
  • 2021-08-14
相关资源
最近更新 更多