如何在基于Django类的View中的方法之间传递变量 [英] How to pass a variable between methods inside Django class based View

查看:74
本文介绍了如何在基于Django类的View中的方法之间传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以从表单获取数据,进行一些更改并将其保存到数据库的类.

I have a class that gets the data from the form, makes some changes and save it to the database.

我想里面有几种方法.

  • 获取
  • 发布
  • 以及其他一些将对表单中的数据进行更改的方法

我希望post方法将数据从表单保存到数据库,并将instans变量传递给下一个方法.下一个方法应该进行一些更改,将其保存到数据库中并返回重定向.

I want the post method to save the data from the form to the database and pass the instanse variable to the next method. The next method should make some changes, save it to the databese and return redirect.

但是我有一个错误.站点"对象没有属性获取"这是我的代码:

But I have an error. 'Site' object has no attribute 'get' Here is my code:

class AddSiteView(View):
    form_class = AddSiteForm
    template_name = 'home.html'

    def get(self, request, *args, **kwargs):
        form = self.form_class()
        return render(request, self.template_name, { 'form': form })
    
    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        if form.is_valid():
            # Get website url from form
            site_url = request.POST.get('url')
            
            # Check if the site is in DB or it's a new site
            try:
                site_id = Site.objects.get(url=site_url)
            except ObjectDoesNotExist:
                site_instanse = form.save()
            else:
                site_instanse = site_id
            return site_instanse
        
        return render(request, self.template_name, { 'form': form })

    def get_robots_link(self, *args, **kwargs):

        # Set veriable to the Robot Model
        robots = Robot.objects.get(site=site_instanse)

        # Robobts Link
        robots_url = Robots(site_url).get_url()
        robots.url = robots_url
        robots.save()    
        return redirect('checks:robots', robots.id, )

我需要将site_instanse从def post传递到def get_robots_link

I need to pass site_instanse from def post to def get_robots_link

这是回溯:

Internal Server Error: /add/
Traceback (most recent call last):
  File "/home/atom/.local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/atom/.local/lib/python3.8/site-packages/django/utils/deprecation.py", line 96, in __call__
    response = self.process_response(request, response)
  File "/home/atom/.local/lib/python3.8/site-packages/django/middleware/clickjacking.py", line 26, in process_response
    if response.get('X-Frame-Options') is not None:
AttributeError: 'Site' object has no attribute 'get'
[14/Jul/2020 10:36:27] "POST /add/ HTTP/1.1" 500 61371

这是问题所在的地方:如果我在post方法中使用redirect.一切正常.像这样:

Here is the place where the problem is: If I use redirect inside the post method. Everything works good. Like so:

class AddSiteView(View):
    form_class = AddSiteForm
    template_name = 'home.html'

    def get(self, request, *args, **kwargs):
        form = self.form_class()
        return render(request, self.template_name, { 'form': form })
    
    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        if form.is_valid():
            # Get website url from form
            site_url = request.POST.get('url')
            
            # Check if the site is in DB or it's a new site
            try:
                site_id = Site.objects.get(url=site_url)
            except ObjectDoesNotExist:
                site_instanse = form.save()
            else:
                site_instanse = site_id
            
            # Set veriable to the Robot Model
            robots = Robot.objects.get(site=site_instanse)

            # Robobts Link
            robots_url = Robots(site_url).get_url()
            robots.url = robots_url
            robots.save()    
            return redirect('checks:robots', robots.id, ) ## HERE
        
        return render(request, self.template_name, { 'form': form })

但是,如果从post方法中删除行 return redirect('checks:robots',robots.id,),然后将return self.site_instance放在此处.并添加def get_robots_link.它给出错误:'Site'对象没有属性'get'

But if remove the line return redirect('checks:robots', robots.id, ) from the post method and put return self.site_instance there. and add the def get_robots_link. It give the error: 'Site' object has no attribute 'get'

推荐答案

class AddSiteView(View):
    form_class = AddSiteForm
    template_name = 'home.html'

    def get(self, request, *args, **kwargs):
        form = self.form_class()
        return render(request, self.template_name, { 'form': form })
    
    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        if form.is_valid():
            # Get website url from form
            site_url = request.POST.get('url')
            
            # Check if the site is in DB or it's a new site
            try:
                site_id = Site.objects.get(url=site_url)
            except ObjectDoesNotExist:
                site_instanse = form.save()
            else:
                site_instanse = site_id
                self.site_instance = site_instanse #See this
            return site_instanse
        
        return render(request, self.template_name, { 'form': form })

    def get_robots_link(self, *args, **kwargs):

        # Set veriable to the Robot Model
        robots = Robot.objects.get(site=self.site_instance)

        # Robobts Link
        robots_url = Robots(site_url).get_url()
        robots.url = robots_url
        robots.save()    
        return redirect('checks:robots', robots.id, )

使用 self 对对象内的数据进行编码

Use self to encode the data within the object

这篇关于如何在基于Django类的View中的方法之间传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆