我已经建立了地址模型,地址列表和创建地址。我的问题是创建视图不会将浏览器重定向到success_url。正确显示创建视图,提交后,新地址将添加到数据库,但地址不会更改。因此,用户认为它失败了,因此再次点击提交。直接更改浏览器IP地址将显示多次创建的地址。我创建了一个简单的项目并在其中重复此代码,浏览器会按预期重定向到列表视图。我也试过覆盖get_success_url(self)而没有任何改变。我已经改变了success_url,再没有改变。我知道这在开发的早期阶段起作用,但在最终测试中发现它已经破裂。
这是我的控制台输出,以初始地址列表视图开始。
[13/Apr/2018 12:42:57] "GET /accounts/my_address/ HTTP/1.1" 200 4853
[13/Apr/2018 12:43:01] "GET /accounts/create-address/ HTTP/1.1" 200 5715
[13/Apr/2018 12:43:11] "POST /accounts/create-address/ HTTP/1.1" 302 0
[13/Apr/2018 12:43:11] "GET /accounts/my_address/ HTTP/1.1" 200 5898
控制台日志说它正在做正确的事情,但浏览器地址栏是/ accounts / create-address /而不是/ accounts / my_address /
我的问题是我如何调试它来确定浏览器没有改变的原因,即
models.py
class Address(models.Model):
"""
Allow an account to have multiple addresses for which one
must be active. Shipments will be sent to the active address.
"""
STREET_LENGTH = 128
CITY_LENGTH = 64
STATE_LENGTH = 32
POSTAL_CODE_LENGTH = 10
account = models.ForeignKey(Account, on_delete=models.CASCADE)
active = models.BooleanField(help_text='Select the Active address')
street = models.CharField(max_length=STREET_LENGTH)
city = models.CharField(max_length=CITY_LENGTH)
state = models.CharField(max_length=STATE_LENGTH)
postal_code = models.CharField(max_length=POSTAL_CODE_LENGTH)
forms.py
class AddressForm(forms.ModelForm):
model = Address
street = forms.CharField(max_length=Address.STREET_LENGTH, label='Street:',
required=False, widget=forms.TextInput(
attrs={'class': 'form-control', 'autofocus': 'autofocus'}),)
postal_code = forms.CharField(max_length=Address.POSTAL_CODE_LENGTH, label='Postal Code:',
required=False, widget=forms.TextInput(attrs={'class': 'form-control postal_code'}))
city = forms.CharField(max_length=Address.CITY_LENGTH, label='City:',
required=False, widget=forms.TextInput(attrs={'class': 'form-control city'}))
state = forms.CharField(max_length=Address.STATE_LENGTH, label='State:',
required=False, widget=forms.TextInput(attrs={'class': 'form-control state'}))
urls.py
urlpatterns = [
path('my_address/', AddressListView.as_view(), name='my_address'),
path('create-address/', AddressCreateView.as_view(),
name="create-address"),
]
views.url
class AddressListView(LoginRequiredMixin, ListView):
model = Address
def get_queryset(self):
account = self.request.user
addresses = Address.objects.filter(account=account).order_by('-active')
return addresses
def get_context_data(self, *, object_list=None, **kwargs):
context = super(AddressListView, self).get_context_data(**kwargs)
account = self.request.user
context['account'] = account
return context
class AddressCreateView(LoginRequiredMixin, SuccessMessageMixin, CreateView):
"""
View to create new account address
"""
template_name = "accounts/address_create.html"
form_class = AddressForm
model = Address
success_url = reverse_lazy('accounts:my_address')
success_message = "Address was added successfully"
def form_valid(self, form):
"""
Assign the account to the request.user
"""
form.instance.account = self.request.user
form.instance.active = False
return super(AddressCreateView, self).form_valid(form)
答案 0 :(得分:0)
我发现问题出在HTML文件中,有一些剩余的AJAX。我正在尝试一些事情,我在HTML的form标签类中添加了address_form,并进行了AJAX调用。因此,AJAX和HTTP调用都会发生。我从表单标签的类中删除了address_form,问题就消失了。
<form class="form-horizontal" method="post">
更改为:
$(document).ready(function () {
var $addressForm = $('.address_form')
$addressForm.submit(function(event){
event.preventDefault()
var $formData = $(this).serialize()
var $endpoint = $addressForm.attr("data-url")
$.ajax({method: "POST", url: $endpoint,
data: $formData,
success: handleFormSuccess,
error: handleFormError,
})
})
以后是一些javascript
putchar