Django:form_valid和get_full_path()错误 [英] Django : error with form_valid and get_full_path()

查看:69
本文介绍了Django:form_valid和get_full_path()错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过表格获取当前租户的域名.我很难写出可以实现这一目标的观点.

I am trying to acquire the current tenant's domain name through a form. I am trouble with writing the view that would achieve that.

这是我的form.py:

here is my form.py:

class ETL(forms.Form):
    Historical = forms.FileField()
    Pre_processing = forms.FileField()
    Supplier = forms.FileField()
    parameters = forms.FileField()


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

        url = self.request.get_full_path()

        dbschema = remove_www(url.split(':')[0]).lower()
        engine = create_engine('postgresql://pierre:56-Pmtmpmtm@127.0.0.1:5432/dbexostock12',
                               connect_args={'options': '-csearch_path={}'.format(dbschema)})

        fh = io.TextIOWrapper(self.cleaned_data['Historical'].file)
        fpp = io.TextIOWrapper(self.cleaned_data['Pre_processing'].file)
        fs = io.TextIOWrapper(self.cleaned_data['Supplier'].file)
        fp = io.TextIOWrapper(self.cleaned_data['parameters'].file)

        ....

view.py

@method_decorator(login_required, name='dispatch')
class Getfiles(LoginRequiredMixin,FormView):
    template_name = 'upload.html'
    form_class = ETL
    success_url = 'Home'

    def form_valid(request, form):
        url = request.get_full_path()
        form.process_data()

        return super().form_valid(form)

这是我得到的错误:

AttributeError at /upload.html
'Getfiles' object has no attribute 'get_full_path'

我是django的初学者,我不确定如何解决这个问题,关于它的文档让我有些困惑,尤其是关于form_valid()方法.谢谢!

I am beginner with django, I am not sure how to resolve this, and the documentation about it got me a bit confused, especially about the form_valid() method. Thank you!

推荐答案

form_valid 没有 request 作为第一个参数.参数是 self form .您可以使用 self.request :

form_valid has no request as first parameter. The parameters are self and form. You can access the request object with self.request:

class GetfilesView(LoginRequiredMixin, FormView):
    template_name = 'upload.html'
    form_class = ETL
    success_url = 'Home'

    def form_valid(self, form):
        url = self.request.get_full_path()
        form.process_data()

        return super().form_valid(form)

请注意,您的表单也没有 request .您可以例如通过方法参数传递数据:

Note that your form has no request either. You can pass data, for example through the method parameters:

class GetfilesView(LoginRequiredMixin, FormView):
    template_name = 'upload.html'
    form_class = ETL
    success_url = 'Home'

    def form_valid(self, form):
        url = self.request.get_full_path()
        form.process_data(url)

        return super().form_valid(form)

,其格式为:

class ETL(forms.Form):
    Historical = forms.FileField()
    Pre_processing = forms.FileField()
    Supplier = forms.FileField()
    parameters = forms.FileField()

    def process_data(self, url):
        dbschema = remove_www(url.split(':')[0]).lower()
        engine = create_engine('postgresql://pierre:56-Pmtmpmtm@127.0.0.1:5432/dbexostock12',
                               connect_args={'options': '-csearch_path={}'.format(dbschema)})

        fh = io.TextIOWrapper(self.cleaned_data['Historical'].file)
        fpp = io.TextIOWrapper(self.cleaned_data['Pre_processing'].file)
        fs = io.TextIOWrapper(self.cleaned_data['Supplier'].file)
        fp = io.TextIOWrapper(self.cleaned_data['parameters'].file)

这篇关于Django:form_valid和get_full_path()错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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