私は次のことをしたい:
network_form.html
2 つのフィールドを含むフォームに入力します (form.py を参照)。subnet_network.html
from.py を見てみましょう:
class SubnetCreateFrom(forms.Form):
Subnet_Address = forms.CharField(max_length=40)
IP_Address = forms.CharField(max_length=40)
これらのフォーム フィールドを送信し、これらのフィールドに基づいていくつかのクエリを処理したいと考えています。だから、私は次のvies.pyを書きます:
def subnet_network(request):
if request.method == 'POST':
form = SubnetCreateFrom(request.POST)
if form.is_valid():
subnet = form.data['Subnet_Address']
ip = form.data['IP_Address']
# Here i am manipulating the data using the above subnet and ip fields and pass the data in the form of parameter to the follwing method. i.e. get_host_list
subnet_network_detail(request,get_hosts_list) #[1]
return HttpResponseRedirect('../../list/')
extra_context = {
'form': SubnetCreateFrom(initial={'user': request.user.pk})
}
return direct_to_template(request, 'networks/network_form.html',
extra_context)
テンプレート network_form.html:
<form action="" method="POST">
{{ form.as_p }}
<input type="submit" value="{% trans "Show Host" %}"/>
</form>
送信ボタンをクリックした後、ユーザーがフォームに入力したとき。処理データを表示する新しいページを開く必要がありますget_host_list
。そのため、上記で定義した subnet_network メソッドの途中から subnet_network_detail メソッドを呼び出して、そのデータを次のテンプレートにレンダリングできるようにしました。これは正しい方法ですか?
subnet_network_detail の定義は次のとおりです: (これも同じ views.py にあります)
def subnet_network_detail(request,list_hosts):
extra_context = {
'subnet_hosts': list_hosts
}#[2]
return direct_to_template(request, 'networks/subnet_network.html',
extra_context)
上記のメソッドの URL パターンは次のとおりです。
url(r'^myapp/netmask/create/$',
'subnet_network', name='subnet_network'),
url(r'^myapp/netmask/select/$',
'subnet_network_detail', name='subnet_network_detail')
URL パターンに問題がありますか。SubnetCreateForm
から別のテンプレートにアクセスするデータをレンダリングできないのはなぜですかsubnet_network.html
。これは、そのメソッドを呼び出すか、データをテンプレートにレンダリングする正しい方法ですか?
pdbを別の場所に置いて確認すると、次のことが起こっています。
[1] I used the python debugger at this position and it shows an error i.e. subnet_network_detail receiving one parameter and you are passing it two
[2] After removing the pdb syntax from [1] and putting at this position i check whether the data is passing to this function or not and it was. I am getting the data what i want in list_hosts variable
And also after removing the pdb syntax from [2] position it redirect to the page what we have written in HttpResponseRedirect in the first method