1

I recently started an internship at a tech company with a huge codebase and am currently trying to pull a value from the frontend (written in JS) and pass it to the backend of the app (written in Django). I can successfully access the value in the frontend, but now I'm not sure what file this JS is associated with in the backend, so I don't know how to pass it through to the backend. Is there a suggested way to figure this out? Sorry for such an amateur question but this is all new to me and I'm very confused!

user3802348
  • 2,502
  • 11
  • 36
  • 49
  • Can you add additional informations? How is the frontend structured? When do you need to pass the value from the frontend to the backend? What kind of value is that? – patrick Jan 23 '16 at 00:13
  • @patrick I'm not familiar with typical frontend structures, but I know this frontend is built using Backbone/Marionette and handlebars templates. I need to pass the value (which is a string) from a modal in the frontend (accessed in a .js file) to a backend python function that will check whether the value is valid or not. – user3802348 Jan 25 '16 at 06:42

1 Answers1

1

In django, the URL endpoints (routing) for sending data to (via POST/GET) or navigate to (via browser) are specified in files named urls.py. There is a main urls.py file which is located in same the folder that contains settings.py and wsgi.py, and it contains code that maps the requested URL to other urls.py files. The other urls.py files map the requested URL to functions defined within views.py which render the page or handle data submissions (they may be called 'controllers' in other frameworks).

Based on what your URL currently is, look through each urls.py file starting with the main urls.py until you find the views.py file that the url maps to.

For example, if my current url is /profile

# MyApp/urls.py (note the include('profile.urls') argument)
urlpatterns = patterns('',  
    url(r'^profile/', include('profile.urls'), name='profile'),
    ...
)



# profile/urls.py
from profile import views
urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),      # /profile (this url is matched)
    url(r'^ajax/$', views.ajax, name='ajax'),   # /profile/ajax
    url(r'^user/$', views.user, name='user')    # /profile/user
)



# profile/views.py
def index(request):
    context = RequestContext(request)
    context_dict = {
        'user':request.user,
        'numCheckIns':Event.objects.filter(CheckedInParticipants__id=request.user.id).count,
        'numCommits':Event.objects.filter(participants__id=request.user.id).count,
        'gameLog': Event.objects.filter(checkedInParticipants__id=request.user.id)
    }
    return render_to_response('profile/index.html', context_dict, context)



def ajax(request):
    if request.method == 'POST':
        ...
def user(request):
    ...

The file that correlates with the rendered view at /profile can be traced to profile/views.py and the function that is executed is called index contained within.

James Choi
  • 1,078
  • 14
  • 14