以下视图按预期工作
class BrandEditView(PermissionRequiredMixin ,generic.UpdateView):
permission_required = 'change_brand'
template_name = 'brand_update_form.pug'
model = Brand
fields = ['name']
def get_object(self, queryset=None):
print(self.request.user)
self.object = Brand.objects.get(id=self.kwargs['pk'])
obj = Brand.objects.get(id=self.kwargs['pk'])
return obj
表单提交后,如何返回相同的视图(使用相同的对象),但是使用上下文,如消息:"品牌编辑成功" /"您可以'做那个"?我找到了一种重定向到相同视图的方法,但没有使用上下文。
答案 0 :(得分:1)
在基于类的视图中使用消息框架我们使用SuccessMessageMixin
所以请将您的观点更新为:
#!/bin/bash
function add_user () {
while true; do
echo -e "\nInsert a name for the Squid3 user (0=exit): \c"
read utente
case "$utente" in
0)
echo -e "\nGoodbye $USER!\n"
exit 0
;;
*\ *)
echo -e "\nYou can't use spaces in the name!"
sleep 2
continue
;;
*)
break
;;
esac
done
if [ ! -e '/etc/squid3/.passwd' ]; then
sudo htpasswd -c /etc/squid3/.passwd $utente
else
sudo htpasswd /etc/squid3/.passwd $utente
fi
}
function installer () {
sudo apt-get install squid3 apache2-utils -y
sudo bash -c "echo 'here
you
must
paste
your
configuration
file' > /etc/squid3/squid.conf"
sudo service squid3 restart
}
if ! [ "$(sudo which squid3)" ]; then
installer
add_user
else
add_user
fi
并在基本模板或任何模板中添加:
from django.contrib.messages.views import SuccessMessageMixin
class BrandEditView(SuccessMessageMixin, PermissionRequiredMixin ,generic.UpdateView):
permission_required = 'change_brand'
success_url = reverse_lazy('your redirect url')
template_name = 'brand_update_form.pug'
model = Brand
fields = ['name']
success_message = "created successfully!"
def get_object(self, queryset=None):
print(self.request.user)
self.object = Brand.objects.get(id=self.kwargs['pk'])
obj = Brand.objects.get(id=self.kwargs['pk'])
return obj