我如何.save()到另一个类/函数,如:
def save_page(request):
# contains 3 fields
form = sample_tableform(request.POST)
if request.method == 'POST':
if form.is_valid():
#send a confirmation code to email
#redirect to code confirmation page
def valcode(request):
#contains 1 field
sub_form = confirmform(request.POST)
if request.method == 'POST':
if sub_form.is_valid():
#compare if code from save_pages matches the input field
# if it matches then the data can be stored
form.save()
subform.save()
是否有可能?
我喜欢使用会话的想法,但我该如何实现呢?
答案 0 :(得分:1)
假设您的代码来自views.py
,答案是您不能。
视图不起作用(HTTP也不起作用)。如果要在后续HTTP请求之间访问数据,则必须将其保留在某处。
在您的情况下,您可能需要使用model将验证代码存储在数据库中:
# models.py
from django.db import models
class ValidationCode(models.Model):
code = models.CharField(max_length=10, unique=True)
creation_date = models.DateTimeField(auto_now_add=True)
is_validated = models.BooleanField(default=False)
# views.py
def save_page(request):
# contains 3 fields
form = sample_tableform(request.POST)
if request.method == 'POST':
if form.is_valid():
validation_code = ValidationCode(code="random_code_here")
validation_code.save()
# send email with the code here
#redirect to code confirmation page
def valcode(request):
#contains 1 field
sub_form = confirmform(request.POST)
if request.method == 'POST':
if sub_form.is_valid():
try:
code_from_db = ValidationCode.objects.get(code=form.fields['code'])
except ValidationCode.DoesNotExist:
# Not code matching input exist in database
# return some error to the user
# code exist in db, just mark it as validated
code_from_db.is_validated = True
code_from_db.save()
答案 1 :(得分:0)
<td>{{ country.name }}</td>
<td>{{ country[1960] }}</td>
<td>{{ country[1970] }}</td>
<td>{{ country[1980] }}</td>
<td>{{ country[1990] }}</td>
<td>{{ country[2000] }}</td>