私は自分のview.pyに情報を返すことができるようにHTMLフォームを使いたいと思います。目標はデータを取得し、ストアドプロシージャの呼び出しで引数として使用することです。Djangoがフォームを送信するときにデータベースに書き込む
def mouvementCreation(request):
idMI = 0
especes = TbEspece.objects.order_by('id')
#Get Mouvement informations
#Connection to 'erp-site' DB
cursor = connections['erp-site'].cursor()
try:
#Get Produits list from Espece
query = "{CALL SP_webGET_PRODUIT_FROM_ESPECE(%s,%s,%s,%s,%s)}"
arguments = (2016, 'C', 0, 10, 'A',)
cursor.execute(query, arguments)
produits = dictfetchall(cursor)
#Get Transporters list
cursor.execute("{CALL SP_webGET_TRANSPORT}")
transporters = dictfetchall(cursor)
#Get Livreur list
cursor.execute("{CALL SP_webGET_LIVREUR}")
livreurs = dictfetchall(cursor)
finally:
cursor.close()
cursor = connections['site'].cursor()
try:
#Get Circuit list
cursor.execute("{CALL SP_webGET_CIRCUIT_FOR_MVT}")
circuits = dictfetchall(cursor)
#Get Source list
cursor.execute("{CALL SP_webGET_SOURCE_FOR_MVT}")
mvtsources = dictfetchall(cursor)
#Get Dest list
cursor.execute("{CALL SP_webGET_DEST_FOR_MVT}")
destinations = dictfetchall(cursor)
#Get PontBascule list
cursor.execute("{CALL SP_webGET_PBASCULE}")
pontBascules = dictfetchall(cursor)
finally:
cursor.close()
reg_normes = TbRegauxnormes.objects.all()
ordreexecs = TbOrdreexecution.objects.all()
form = mouvementForm(request.POST or None)
if form.is_valid():
pont = form.cleaned_data['pont']
dateheure = form.cleaned_data['dateheure']
poid = form.cleaned_data['poid']
dsd = form.cleaned_data['dsd']
typepesee = form.cleaned_data['typepesee']
#Connection to 'erp-site' DB
cursor = connections['pontbascule'].cursor()
try:
#Get Produits list from Espece
query = "{CALL SP_ADD_MANUAL_PESEE(%s,%s,%s,%s,%s, %s,%s,%s,%s,%s, %s,%s,%s,%s,%s, %s,%s,%s,%s,%s, %s,%s)}"
arguments = (pont, '11', dateheure, poid, dsd,typepesee, '','','','','','','','','','','','','','','','')
cursor.execute(query, arguments)
finally:
cursor.close()
return render(request, 'mouvementCreation.html', {'form': form, 'especes' : especes, 'produits' : produits, 'transporters' : transporters, 'livreurs' : livreurs, 'circuits' : circuits, 'mvtsources' : mvtsources, 'destinations' : destinations, 'pontBascules' : pontBascules})
ストアドプロシージャは、新しいエントリを作成するはずです。 私は何をしたいのですか?可能であれば確信が持てません:
記入フォーム=>ビューでデータを取得=>取得したデータでストアドプロシージャを呼び出す=>新しいエントリのIDを取得するようにユーザーはurlパラメーターのidをとる別のビューにリダイレクトできます。
これは可能でしょうか?
編集:私のストアドプロシージャと同じように働く投稿要求を得ることができました。私の問題はフォームを提出した後に正しいページにリダイレクトする最後の部分です。
現在のページが/ gestion_mouvement/mouvementCreationであると私は、ユーザーが
問題は、時間によって、クエリが遅すぎるように見えている/ gestion_mouvement/mouvementDetails/{{ID}}にリダイレクトされるようにしたいです私はユーザーが/ gestion_mouvement/mouvementDetails/ にリダイレクトされたフォームを提出し、IDを受け取っていません。
感謝を生成し、私はその部分は私の、私の最後のIDを送信する私のストアドプロシージャで働いてきました問題は、現時点で私がフォームを送信するときに、私は "http:// localhost:8080/gestion_mouvement/mouvementDetails /"というフォームを私が "http:// localhost: 8080/gestion_mouvement/mouvementDetails/{{idCreated}} "しかし、ポストリクエストはクエリよりも速くリダイレクトされるので、IDなしのURLになり、ページに到達することはありません。 –
私はポイントを参照してください。あなたのURL宣言でreverse_lasyを使用しましたか? "_プロジェクトのURLConfがロードされる前にURLの逆転を使用する必要がある場合に便利です。" [django doc](https://docs.djangoproject.com/ja/1.11/ref/urlresolvers/#reverse-lazy) – openHBP
私は、私はドキュメントをチェックしただけで、実際に私の状況でどのように動作するのかわかりません。ページをリロードする前にページがIDを受け取ることが可能になりますか?私は私のフォームを提出する前にIDを知らないので –