在from.py中访问view.py的函数

时间:2017-12-14 14:38:51

标签: django django-forms django-views

如何在form.py?

中访问view.py函数的结果

我正在尝试使用视图函数的结果作为表单字段的输入

这是我的代码

Views.py

def inc(request):
    user_name = request.user.username
    u = User.objects.get(username=user_name)
    cont = u.accesslevel.numb

Admin.py

class AccessLevelInline(admin.StackedInline):
    model = AccessLevel
    can_delete = False
    verbose_name_plural = 'accesslevel'

# Define a new User admin
class UserAdmin(BaseUserAdmin):
    inlines = (AccessLevelInline, )

# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)

Form.py

user_name = HOW DO I ACCESS THE USER NAME FROM VIEWS HERE?
u = User.objects.get(username=user_name).accesslevel.prod.split(',')
v=(('---------','---------'),)
for l in u:
v=v+((l.lstrip(),l.lstrip()),)

class ProductForm(forms.Form):
    model = ManufactForm    
    Prod = forms. ChoiceField(choices=v)

1 个答案:

答案 0 :(得分:0)

假设您的python文件是Form.pyView.py, 在Form.py的顶部,像这样导入View.py

import View

然后您可以调用view.py文件中的任何功能

View.inc()