[英]Django success url using kwargs
I am trying to amend my get_success_url
so that if any kwargs
have been passed to it, I can build the returned url using them.我正在尝试修改我的
get_success_url
以便如果任何kwargs
已传递给它,我可以使用它们构建返回的 url。
Heres what I have so far:这是我到目前为止所拥有的:
class CalcUpdate(SuccessMessageMixin, UpdateView):
model = Calc
template_name = 'calc/cru_template.html'
form_class = CalcForm
def archive_calc(self, object_id):
model_a = Calc.objects.get(id = object_id)
model_b = Calc()
for field in model_a._meta.fields:
setattr(model_b, field.name, getattr(model_a, field.name))
model_b.pk = None
model_b.save()
self.get_success_url(idnumber = model_b.pk)
def form_valid(self, form):
#objects
if self.object.checked == True:
object_id = self.object.id
self.archive_calc(object_id)
#save
def get_success_url(self, **kwargs):
if kwargs != None:
return reverse_lazy('detail', kwargs = {'pk': kwargs['idnumber']})
else:
return reverse_lazy('detail', args = (self.object.id,))
So far this just gives a keyerror
detailing 'idnumber'
.到目前为止,这只是给出了一个详细说明
'idnumber'
keyerror
'idnumber'
。
I have printed kwargs['idnumber']
and it returns the pk
as expected however I just cant seem to see where I am going wrong with this.我已经打印了
kwargs['idnumber']
并且它按预期返回了pk
但是我似乎看不出我哪里出错了。
Thanks in advance.提前致谢。
form_valid
should return a HttpResponseRedirect
https://github.com/django/django/blob/master/django/views/generic/edit.py#L57 which in your case, you never do. form_valid
应该返回一个HttpResponseRedirect
https://github.com/django/django/blob/master/django/views/generic/edit.py#L57 ,在你的情况下,你永远不会这样做。 I dont know if you have any code after #save
, but take a look at the comments I made in your code 我不知道你在
#save
之后是否有任何代码,但是看看我在你的代码中做出的评论
class CalcUpdate(SuccessMessageMixin, UpdateView):
model = Calc
template_name = 'calc/cru_template.html'
form_class = CalcForm
def archive_calc(self, object_id):
model_a = Calc.objects.get(id = object_id)
model_b = Calc()
for field in model_a._meta.fields:
setattr(model_b, field.name, getattr(model_a, field.name))
model_b.pk = None
model_b.save()
return self.get_success_url(idnumber = model_b.pk) # you never return this value
def form_valid(self, form):
#objects
if self.object.checked == True:
object_id = self.object.id
return HttpResponseRedirect(self.archive_calc(object_id)) # you never return a `HttpResponse`
#save -- If this is where you are saving... you can store the value from archive and return it after saving
def get_success_url(self, **kwargs):
if kwargs != None:
return reverse_lazy('detail', kwargs = {'pk': kwargs['idnumber']})
else:
return reverse_lazy('detail', args = (self.object.id,))
Also you don't need to manually copy the fields, just do (assuming there are no unique
constraints because if there were, your version would fail too): 您也不需要手动复制字段,只需这样做(假设没有
unique
约束,因为如果有,您的版本也会失败):
def archive_calc(self, object_id):
c = self.model.objects.get(id = object_id)
c.pk = None
c.save()
return self.get_success_url(idnumber = c.pk)
After playing around with @Ngenator's answer and various other posts on here I have the following working code. 在使用@Ngenator的答案和其他各种帖子后,我有以下工作代码。 However its not very nice to look at :(
然而它看起来不是很好:(
def get_success_url(self):
if self.pknumber != None:
return reverse_lazy('pstdetail', args = (self.pknumber,))
else:
return reverse_lazy('pstdetail', args = (self.object.id,))
I have this self.pknumber = model_b.pk
in the necessary place within the view and self.pknumber = None
else where to enable the if statement to build the required url. 我在视图中的必要位置有
self.pknumber = model_b.pk
和self.pknumber = None
其他在哪里启用if语句来构建所需的url。 Hope this helps anyone and feel free to point out any errors/improvements. 希望这可以帮助任何人,并随时指出任何错误/改进。
https://ccbv.co.uk/projects/Django/4.0/django.views.generic.edit/UpdateView/ https://ccbv.co.uk/projects/Django/4.0/django.views.generic.edit/UpdateView/
You cannot pass parameters in get_success_url(self)
method.您不能在
get_success_url(self)
方法中传递参数。 You can only refer to self
parameter.您只能参考
self
参数。 For example self.kwargs['pk']
.例如
self.kwargs['pk']
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.