如何判断哪些后端文件与前端文件相关联?

How to tell which backend files are associated with frontend files?

我最近开始在一家拥有庞大代码库的科技公司实习,目前正试图从前端(用 JS 编写)提取一个值并将其传递到应用程序的后端(用 Django 编写)。我可以在前端成功访问该值,但是现在我不确定这个JS在后端关联的是什么文件,所以我不知道如何将它传递到后端。有没有建议的方法来解决这个问题?很抱歉提出这样一个业余问题,但这对我来说是全新的,我很困惑!

在 django 中,用于将数据发送到(通过 POST/GET)或导航到(通过浏览器)的 URL 端点(路由)在名为 urls.py 的文件中指定。有一个主 urls.py 文件位于包含 settings.pywsgi.py 的同一文件夹中,它包含将请求的 URL 映射到其他 urls.py 的代码] 文件。其他 urls.py 文件将请求的 URL 映射到 views.py 中定义的函数,这些函数呈现页面或处理数据提交(在其他框架中它们可能被称为 'controllers')。

根据您的 URL 目前的情况,查看从主要 urls.py 开始的每个 urls.py 文件,直到找到 views.py 文件 url 映射到.

例如,如果我当前的 url 是 /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):
    ...

/profile 处的渲染视图关联的文件可以追溯到 profile/views.py,执行的函数称为 index 包含在其中。