我正在使用' post'提交表单。并使用request.POST将其数据传输到另一个视图,但是当它到达第二个视图时我的querydict不完整。
厂景
def question_filter(request):
if request.method == 'POST':
print('before validation', request.POST)
request.session['question_data'] = request.POST
return HttpResponseRedirect(reverse('qapp:question_preview'))
视图2
def question_preview(request):
all_questions = Questions.objects.all()
question_data = request.session.get('question_data')
print(question_data)
question_pk_list = question_data['question_pk']
preview_questions = all_questions.filter(id__in=question_pk_list)
...
return render(request,'apps/qapp/question_preview.html', {somecontext})
我在这里做错了吗?
更新
before validation <QueryDict: {'topics_all': ['1', '2'], 'csrfmiddlewaretoken': ['...'], 'subtopics_all': ['4', '2'], 'classroom': ['3'], 'difficulty': ['l', 'm']}>
[28/Feb/2018 17:17:39] "POST /question/filter/ HTTP/1.1" 302 0
(in the second view)question data {'topics_all': '2', 'csrfmiddlewaretoken': '...', 'difficulty': 'm', 'subtopics_all': '2', 'classroom': '3'}
答案 0 :(得分:1)
默认情况下不保存会话。添加request.session.modified = True
:
def question_filter(request):
if request.method == 'POST':
print('before validation', request.POST)
request.session['question_data'] = request.POST
request.session.modified = True
return HttpResponseRedirect(reverse('qapp:question_preview'))
https://docs.djangoproject.com/en/2.0/topics/http/sessions/#when-sessions-are-saved
修改强>
你可以使用json破解它:
import json
def question_filter(request):
if request.method == 'POST':
print('before validation', request.POST)
request.session['question_data'] = json.dumps(request.POST)
return HttpResponseRedirect(reverse('qapp:question_preview'))
def question_preview(request):
all_questions = Questions.objects.all()
question_data = json.loads(request.session.get('question_data'))
print(question_data)
question_pk_list = question_data['question_pk']
preview_questions = all_questions.filter(id__in=question_pk_list)
...
return render(request,'apps/qapp/question_preview.html', {somecontext})
答案 1 :(得分:1)
您无法像import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import fmin_cobyla
P = (2487,1858)
def f(x):
return (1.77425666496e-05 * x**3 + -0.125555135823 * x**2 + 296.656015233 * x + -232082.382985)
def objective(x):
return np.sqrt((x[0] - P[0])**2 + (f(x[0]) - P[1])**2)
def c1(x):
return 0.0
x_c = fmin_cobyla(objective, x0=2300, cons=c1)
X = [x_c , f(x_c)]
print ("The minimum distance is {0:1.2f}".format(objective(X)))
v1 = np.array(P) - np.array(X)
v2 = np.array([1, 2.0 * X[0]])
print ("dot(v1, v2) = ",np.dot(v1, v2))
x = np.linspace(1000, 3000, 500000)
plt.plot(x, f(x), 'r-', label='f(x)')
plt.plot(P[0], P[1], 'bo', label='point')
plt.plot([P[0], X[0]], [P[1], X[1]], 'b-', label='shortest distance')
plt.plot([X[0], X[0] + 1], [X[1], X[1] + 2.0 * X[0]], 'g-', label='tangent')
plt.axis('equal')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
那样发送所有post data
,因为您必须以不同的方式访问每个名称,并将其设置在list inside your input names,
。
session
与其他request.session['question_data'] = request.POST.getlist('topics_all')
相同,然后在第二个视图中使用键进行访问