我的网站上有2个按钮 - 1.编辑条目2.删除条目。 问题是,在我单击确认删除或编辑后,页面刷新并获取所有新信息,但它从头开始加载页面(页面上方)
由于我的页面很长,有很多条目....我想在点击“确认”之后,它会将用户重定向到他在页面中点击的同一位置,这样他就不需要滚动鼠标一次又一次地沿着页面走下去..
有谁知道?
views.py -
class HomeView(TemplateView):
template_name = 'serverlist.html'
def get(self, request):
form = HomeForm()
query = request.GET.get("q")
posts = serverlist.objects.all()
forms = {}
if query:
posts = serverlist.objects.filter(Q(ServerName__icontains=query) | Q(Owner__icontains=query) | Q(Project__icontains=query) | Q(Description__icontains=query) | Q(IP__icontains=query) | Q(ILO__icontains=query) | Q(Rack__icontains=query))
else:
posts = serverlist.objects.all()
for post in posts:
forms[post.id] = HomeForm(instance=post)
args = {'form' : form,'forms': forms, 'posts' : posts}
return render(request, self.template_name,args)
def post(self,request):
form = HomeForm(request.POST)
posts = serverlist.objects.all()
forms = {}
if form.is_valid(): # Checks if validation of the forms passed
post = form.save(commit=False)
post.save()
messages.success(request,'{0} has been added successfully!'.format(post.ServerName))
return redirect('serverlist')
messages.error(request,'Servername is required, please refresh the page and try again.')
for post in posts:
forms[post.id] = HomeForm(instance=post)
args = {'form' : form, 'forms': forms, 'posts' : posts}
return render(request, self.template_name,args)
class PostDelete(SuccessMessageMixin,DeleteView):
model = serverlist
success_url = reverse_lazy('serverlist')
success_message = "The server has been deleted successfully!"
def delete(self, request, *args, **kwargs):
messages.warning(self.request, self.success_message)
return super(PostDelete, self).delete(request, *args, **kwargs)
class PostEdit(SuccessMessageMixin,UpdateView):
template_name = 'serverlist.html'
model = serverlist
form_class = HomeForm
success_url = reverse_lazy('serverlist')
success_message = "%(ServerName)s has been edited successfully!"
def edit(self, request, *args, **kwargs):
messages.warning(self.request, self.success_message)
return super(PostEdit, self).edit(request, *args, **kwargs)