0

I have a Django project that I am building. Within the django project I have two apps. one is users and one is accounts. I want to call a view from the acounts app called user_synapse in the users app. I have imported the accounts views python file to import all the methods. I am then calling the method but I am getting an error that says the view method that I am trying to call is not defined. It was working earlier but now it is not working at all. I added the django app name to the settings file and the main urls file. Here is the code I have:

Users app:

views.py file:

#import all references from other apps
from accounts.views import *
from groups.models import *
from groups.views import *
...
# create synapse user
user_synapse(request)

accounts app:

views.py file:

#import all references from other apps
from groups.models import *
from groups.views import *
from users.views import *
from users.models import *

# synapse import statements that are needed
from synapse_pay_rest import Client, Node, Transaction
from synapse_pay_rest import User as SynapseUser
from synapse_pay_rest.models.nodes import AchUsNode
...
# ensure someone is logged in
@login_required
# create a synapse user
def user_synapse(request):
    # grab the logged in user
    user = request.user
    # grab users profile
    profile = Profile.objects.get(user = user)
    # set user items before creating items
    legal_name = profile.first_name + " " + profile.last_name
    note = legal_name + " has just created his synapse profile "
    supp_id = generate_number()
    cip_tag = user.id
    # grab the account type
    account = profile.business
    if account == 'INDIVIDUAL':
        account = False
    if account == 'BUSINESS':
        account = True
    # the following is all of the information that is required in order to make a
    # new user within the Synapse application
    args = {
        'email':str(user.email),
        'phone_number':str(profile.phone),
        'legal_name':str(legal_name),
        'note': str(note),
        'supp_id':str(supp_id),
        'is_business':account,
        'cip_tag':cip_tag,
    }
    # the following is the request to the synapse api as well as the returned
    # json that contains information that needs to ba saved in local database
    create_user = SynapseUser.create(client, **args)
    response = create_user.json
    # the following updates the current profile to add the users synapse id within
    # the local database.
    if response:
        synapse_id = response['_id']
        updateProfile = profile
        updateProfile.synapse_id = synapse_id
        updateProfile.save()

the view does exist. why in the world is it saying that the method is not defined:

NameError at /personal/
name 'user_synapse' is not defined

settings.py file:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'general',
    'users',
    'localflavor',
    'groups',
    'accounts',
]

It was working earlier but now it is not working and I have no idea why.

Omar Jandali
  • 814
  • 3
  • 20
  • 52
  • did you add the view in the accounts urls.py file? I think you need to check the correct urls for your apps – akimul Nov 23 '17 at 12:19
  • Not really an answer to your problem, but it seems that you have an import problem somewhere... Avoiding `import *` is the first step to avoid this kind of problem. You might want to read: https://stackoverflow.com/questions/2386714/why-is-import-bad – Clément Denoix Nov 23 '17 at 12:20
  • yes this is what it looks like `url(r'^admin/', admin.site.urls), url(r'^accounts/', include('accounts.urls')), url(r'^groups/', include('groups.urls')), url(r'^', include('users.urls')),` – Omar Jandali Nov 23 '17 at 12:21
  • I tried typing it out in the port statement and it said that it couldnt be found... – Omar Jandali Nov 23 '17 at 12:22
  • 1
    the error says '/personal/' but I dont see any personal url settings – akimul Nov 23 '17 at 12:26
  • personal is the view that contains the call to synapse user. first block of code. the error is coming from the line that I have in the first block of cdoe callling `user_synapse(request)` – Omar Jandali Nov 23 '17 at 12:37
  • can you try the import with `from accounts.views import user_synapse` – akimul Nov 23 '17 at 12:38
  • I already tried it and it says `cannot import name 'user_synapse'` – Omar Jandali Nov 23 '17 at 12:40
  • I dont think this is the issue - but can it be the app name accounts is a keyword i cant user or should it not matter -- @akimul – Omar Jandali Nov 23 '17 at 12:42
  • May be it can solve your issue. There is a circular dependency in your views. You are using `from users.views import *` in accounts.views file and `from accounts.views import *` in users.views file. You can remove `from users.views import *` from accounts.views if not needed. – akimul Nov 23 '17 at 12:56

1 Answers1

1

The problem is that you have a circular import: in users.views.py

from accounts.views import *

in accounts.views.py

from users.views import *

This is not permitted in Python. See import dependency in Python for more info.

Vaibhav
  • 1,154
  • 10
  • 26