[英]Python Django POST request to different views
In Python Django, I sent a POST request to one view, called view1, and want to see that same POST request in another view, called view2. 在Python Django中,我向一个名为view1的视图发送了一个POST请求,并希望在另一个视图中看到相同的POST请求,称为view2。
I tried to import that view1's method into view2, yet I just got that view1's method when doing a GET request to view2, not a POST request from view1 and seeing it in view2. 我试图将view1的方法导入到view2中,但是我在对view2执行GET请求时得到了view1的方法,而不是来自view1的POST请求并在view2中查看它。
view1.py view1.py
@csrf_exempt
def index(request):
if "POST" == request.method:
list_data = json.loads(request.body.decode('utf-8'))
elif "GET" == request.method:
list_data = serialize('json', ApiStatus.objects.all())
return HttpResponse(list_data, content_type='application/json')
view2.py view2.py
from app.views.view1 import index
def status(request):
print("STATUS request method ", request) # gives a GET request when reloading page
print("INDEX method from view1.py ", index) # shows when doing GET request
return render(request, 'template.html')
post_file.py post_file.py
def get_test_req():
return { 'Samurai': 'Masamune'}
def post_data():
data = []
data.append(get_test_req())
headers = {'Content-Type': 'application/json', 'Accept':'application/json'}
r = requests.post(view1_url, headers=headers, json=data, timeout=10.0)
Expected: To see post request from view1 into view2 after posting to view1. 预期:在发布到view1后查看从view1到view2的发布请求。
Actual: I only see a GET request in view2 and showing view1 as an imported value. 实际:我只在view2中看到一个GET请求,并将view1显示为导入值。
I was able to figure out a work around. 我能够找到一个解决方法。 Instead of looking for different post requests, in my initial post request in the first view, I added values to a database.
我在第一个视图中的初始post请求中没有查找不同的post请求,而是将值添加到数据库中。
In the second view, I did a GET request and then checked if values were updated in a database in the last minute. 在第二个视图中,我做了一个GET请求,然后检查在最后一分钟是否在数据库中更新了值。 If there were values in the queryset, I'd initialize the ROS node with code as follows:
如果查询集中有值,我将使用以下代码初始化ROS节点:
view2.py view2.py
def status(request):
right_now = datetime.now()
last_time = right_now - timedelta(seconds=20)
last_min_data = ApiStatus.objects.values().filter(date_time__gte=last_time)
if last_min_data.exists():
logging.debug("\n \n last_min_data EXISTS \n \n")
rospy.init_node('root')
s = rospy.Service('ros_post', OneInt, handle_request)
If there is a better solution, I'm certainly open to hearing it. 如果有更好的解决方案,我肯定愿意听到它。 Thanks.
谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.