Django 1.8 使用表单在页面之间传递数据
Django 1.8 Passing data between pages with using form
我已经有了代码,但它不起作用。但是我正在寻找有关如何使用表单在页面之间传递数据的解决方案或实践。我应该使用隐藏域还是会话?我已准备好更改或重写我的代码。我打开了其他建议,想到了。
这是我的代码
forms.py
class applyForm(forms.ModelForm):
basvuru_mesaji = forms.CharField(required=True,error_messages={'required':'Bu alanı boş bırakmamanız gerekiyor'}, widget=forms.Textarea(attrs={'class': 'full','placeholder': 'Başvuru mesajınızı buraya yazınız'}))
tcn = forms.IntegerField(required=False, error_messages={'invalid':'Numara dışında karakter girişi yaptınız'}, widget=forms.TextInput(attrs={'class': 'full','placeholder': 'T.C. Kimlik Numaranız'}))
hidden = forms.CharField(widget=forms.HiddenInput(), required = False)
class Meta():
model = apply
fields = '__all__'
models.py
class apply(models.Model):
tcn = models.PositiveIntegerField(blank=True, verbose_name='T.C.N :', null=True)
basvuru_mesaji = models.TextField(verbose_name='Başvuru Mesajı')
views.py
def form_for_apply(request):
form = applyForm(request.POST or None)
if form.is_valid():
new_join = form.save(commit=True)
new_join.save()
basvuru_mesaji = form.cleaned_data["basvuru_mesaji"]
return HttpResponseRedirect('/apply_ok/')
context = {"form":form}
template = "apply.html"
return render(request, template, context)
def apply_ok(request):
allfield = apply.objects.all()
print allfield
form = applyForm(request.GET)
context = {"form":form}
#print context.data
template = "apply_ok.html"
return render(request, template, context)
apply.html
{% csrf_token %}
{% if form.errors %}
{{ form.errors }}
{% endif %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
<div class="mTopTwe">
<label for="{{ form.basvuru_mesaji.id_for_label }}">
{{form.basvuru_mesaji.field.label}}
</label>
{{ form.basvuru_mesaji.errors }}
{{ form.basvuru_mesaji }}
</div>
apply_ok.html
<div class="mTopTwe">
{{form.basvuru_mesaji.data}} {{ form.hidden.data }}
</div>
您可以使用 django wizard-form 在页面之间传递数据。
render_to_response 解决了我的问题。
return render_to_response('apply_ok.html', context_instance=RequestContext(request,{'form':form}))
forms.py
def apply(request):
form = applyForm(request.POST or None)
if form.is_valid():
new_join = form.save(commit=True)
new_join.save()
basvuru_mesaji = form.cleaned_data['basvuru_mesaji']
return render_to_response('apply_ok.html', context_instance=RequestContext(request,{'form':form}))
context = {"form":form}
template = "apply.html"
return render(request, template, context)
我已经有了代码,但它不起作用。但是我正在寻找有关如何使用表单在页面之间传递数据的解决方案或实践。我应该使用隐藏域还是会话?我已准备好更改或重写我的代码。我打开了其他建议,想到了。
这是我的代码
forms.py
class applyForm(forms.ModelForm):
basvuru_mesaji = forms.CharField(required=True,error_messages={'required':'Bu alanı boş bırakmamanız gerekiyor'}, widget=forms.Textarea(attrs={'class': 'full','placeholder': 'Başvuru mesajınızı buraya yazınız'}))
tcn = forms.IntegerField(required=False, error_messages={'invalid':'Numara dışında karakter girişi yaptınız'}, widget=forms.TextInput(attrs={'class': 'full','placeholder': 'T.C. Kimlik Numaranız'}))
hidden = forms.CharField(widget=forms.HiddenInput(), required = False)
class Meta():
model = apply
fields = '__all__'
models.py
class apply(models.Model):
tcn = models.PositiveIntegerField(blank=True, verbose_name='T.C.N :', null=True)
basvuru_mesaji = models.TextField(verbose_name='Başvuru Mesajı')
views.py
def form_for_apply(request):
form = applyForm(request.POST or None)
if form.is_valid():
new_join = form.save(commit=True)
new_join.save()
basvuru_mesaji = form.cleaned_data["basvuru_mesaji"]
return HttpResponseRedirect('/apply_ok/')
context = {"form":form}
template = "apply.html"
return render(request, template, context)
def apply_ok(request):
allfield = apply.objects.all()
print allfield
form = applyForm(request.GET)
context = {"form":form}
#print context.data
template = "apply_ok.html"
return render(request, template, context)
apply.html
{% csrf_token %}
{% if form.errors %}
{{ form.errors }}
{% endif %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
<div class="mTopTwe">
<label for="{{ form.basvuru_mesaji.id_for_label }}">
{{form.basvuru_mesaji.field.label}}
</label>
{{ form.basvuru_mesaji.errors }}
{{ form.basvuru_mesaji }}
</div>
apply_ok.html
<div class="mTopTwe">
{{form.basvuru_mesaji.data}} {{ form.hidden.data }}
</div>
您可以使用 django wizard-form 在页面之间传递数据。
render_to_response 解决了我的问题。
return render_to_response('apply_ok.html', context_instance=RequestContext(request,{'form':form}))
forms.py
def apply(request):
form = applyForm(request.POST or None)
if form.is_valid():
new_join = form.save(commit=True)
new_join.save()
basvuru_mesaji = form.cleaned_data['basvuru_mesaji']
return render_to_response('apply_ok.html', context_instance=RequestContext(request,{'form':form}))
context = {"form":form}
template = "apply.html"
return render(request, template, context)