我正在尝试创建一个Django应用程序,但我有一个问题,模板没有传递它的内容来查看。
这是我的表格..
<form action="{% url 'view-job' %}" method="post" enctype=application/x-www-form-urlencoded>
{% csrf_token %}
<div>
<label for="name">Job name: </label>
<input type="text" id="name" />
</div>
<div>
<label for="owner">Owner: </label>
<input type="text" id="owner" />
</div>
...
这是我的邮政处理视图(views_job.py)
def job(request, pk=None):
if request.method == 'GET':
return get(request, pk)
elif request.method == 'POST':
return post(request)
...
并发布(请求)
def post(request):
#data = json.loads(request.body.decode("utf-8"))
#data = load_json(request)
new_job = create_new_job(request)
#new_job = create_new_job(request)
if new_job != False:
return new_job
else:
return HttpResponse(status=500)
create_new_job(REQ)
def create_new_job(req):
config = parse_config()
try:
queryset = Job.objects.create(
name=req.POST.get('name'),
owner=req.POST.get('owner'),
execute_date=req.POST.get('execute_date'),
created_date=timezone.now(),
master=req.POST.get('master') if 'master' in req.POST != None else config[DEFAULT][MASTER],
deploy_mode=req.POST.get('deploy_mode') if 'deploy_mode' in req.POST != None else config[DEFAULT][DEPLOY_MODE],
conf=req.POST.get('conf'),
classpath=req.POST.get('classpath'),
app_arguments=req.POST.get('app_arguments'),
user_params=req.POST.get('user_params'),
status=READY,
)
#queryset.save()
except:
print("Error")
print(req.POST.get('name'))
return False
return render(req, 'jobUI/job_details.html',
{
#'job':queryset
}
)
控制台打印
INFO 2018-03-21 18:48:29,033 basehttp 1321 140106696156928 "GET /jobUI/job/ HTTP/1.1" 200 1799
INFO 2018-03-21 18:48:30,208 basehttp 1321 140106696156928 "GET /jobUI/job/newjob/ HTTP/1.1" 200 1620
Error
None
ERROR 2018-03-21 18:48:47,499 basehttp 1321 140106696156928 "POST /jobUI/job/ HTTP/1.1" 500 0
我不知道为什么request.POST ['name']是None和其他东西..
我该如何解决?
谢谢。答案 0 :(得分:0)
因为您的input
字段没有name
属性;没有它,浏览器将不会发送任何数据。
注意,您应该真正使用Django表单框架。你绝对不应该有一个空白的except
条款。