自几个小时前以来,我面临一个基本问题,因此在尝试做许多示例之后,我决定问一个问题,但我仍然无法使用POST方法保存表单中的数据。
进入我的模板视图时,当我发布数据并且从网页中什么都没有进入数据库时,即使我执行了print(request.POST),我也一无所获,这似乎毫无用处。
我正在使用Django 2.2的最新版本。
这是我的模特
class AddressDelivery(models.Model):
billing_profile = models.ForeignKey(BillingProfile, on_delete=models.CASCADE)
first_name = models.CharField(max_length=120)
last_name = models.CharField(max_length=120)
address_type = models.CharField(max_length=120, choices=ADDRESS_TYPES)
address_line_1 = models.CharField(max_length=120)
address_line_2 = models.CharField(max_length=120, null=True, blank=True)
city = models.CharField(max_length=120)
phone = PhoneNumberField(unique=True)
country = models.CharField(max_length=120, choices=COUNTRIES, default=current_country)
state_or_department = models.CharField(max_length=120)
postal_code = models.CharField(max_length=120)
def __str__(self):
return str('{}'.format(self.billing_profile))
这是我的表格:
class AddressForm(forms.ModelForm):
class Meta:
model = AddressDelivery
fields = [
'first_name',
'last_name',
'phone',
#'billing_profile',
#'address_type',
'address_line_1',
'address_line_2',
'city',
'postal_code',
'state_or_department',
'country',
]
widgets = {
'first_name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'First Name'}),
'last_name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Last Name'}),
'postal_code': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'postal code'}),
'state_or_department': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'state or department'}),
'city': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'city'}),
'address_type': forms.Select(attrs={'class': 'form-control'}),
'address_line_1': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'address line 1'}),
'address_line_2': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'address line 2'}),
'country': forms.Select(attrs={'class': 'form-control'}),
'address_type': forms.Select(attrs={'class': 'form-control'}),
'phone': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Phone number with country code'}),
}
这是我处理表格的观点:
def checkout_address_create_view(request):
form = AddressForm(request.POST or None)
context = {
"form": form
}
next_ = request.GET.get('next')
next_post = request.POST.get('next')
redirect_path = next_ or next_post or None
if form.is_valid():
print(request.POST)
instance = form.save(commit=False)
billing_profile, billing_profile_created = BillingProfile.objects.new_or_get(request)
if billing_profile is not None:
address_type = request.POST.get('address_type', 'shipping')
instance.billing_profile = billing_profile
instance.address_type = request.POST.get('address_type', 'shipping')
instance.save()
else:
print("Error here")
return redirect("checkout")
if is_safe_url(redirect_path, request.get_host()):
return redirect(redirect_path)
else:
return redirect("checkout")
return redirect("checkout")
这是我的模板表格:
<form method="POST" action="{% if action_url %}{{ action_url }}{% else %}{% url 'login' %}{% endif %}"> {% csrf_token %}
{% if next_url %}
<input type="hidden" name="next" value="{{ next_url }}" />
{% endif %}
{% if address_type %}
<input type="hidden" name="address_type" value="{{ address_type }}" />
{% endif %}
{{ form.as_p }}
提交
此寄存器视图使用相同的方法,并且可以正常工作:
def register(request):
# get_variable_country()
# print(get_variable_country())
get_variable_model()
print(get_variable_model())
if request.user.is_authenticated:
return redirect('index')
elif request.user.is_authenticated and (request.user.is_admin == True):
return redirect('index')
else:
form = RegisterForm(request.POST or None)
context = {
'form': form
}
if form.is_valid():
form.save()
return redirect('thanks')
return render(request, 'accounts/register.html', context)
请帮助谢谢。