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!
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.